统计

  • 随笔 - 50
  • 文章 - 42
  • 评论 - 147
  • 引用 - 0

留言簿(6)

随笔分类

文章分类

Link

搜索

  •  

积分与排名

  • 积分 - 160264
  • 排名 - 161

最新评论

阅读排行榜

评论排行榜

C++对象内存模型
/*
 *C++中非多态类对象的内存映像模型
 *
 *
 *
 */
#include <iostream>

using namespace std;

class rectangle
{
    public:
  rectangle():m_length(1), m_width(1){}
  ~rectangle(){}
  float GetLength() const
  {
      return m_length;
  }
  void SetLength(float length)
  {
      m_length = length;
  }
  float GetWidth() const
  {
      return m_width;
  }
  void SetWidth(float width)
  {
      m_width = width;
  }
  void Draw() const 
  {
      //...
      //...
      //...
  }
  static unsigned int GetCount()
  {
      return m_count;
  }

    protected:
  rectangle(const rectangle & copy)
  {
      //...
      //...
      //...
  }
  rectangle& operator=(const rectangle & assign)
  {
      //...
      //...
      //...
  }

    private:
  float m_length;
  float m_width;
  static unsigned int m_count;
};
int main()
{
    rectangle rect;
    rect.SetWidth(10);
    cout<<rect.GetWidth()<<endl;
    return 0;
}
/*
 *有如下规则:
非静态数据成员被放在每一个对象体内作为对象专有的数据成员
静态数据成员被提取出来放在程序的静态数据区内,为该类所有对象共享,因此只存在一份。
静态和非静态成员函数最终都被提取出来放在程序的代码段中并为该类所有对象共享,因此每一个成员函数也只能存在一份代码实体。
因此,构成对象本身的只有数据,任何成员函数都不隶属于任何一个对象,非静态成员函数与对象的关系就是绑定,绑定的中介就是this指针。
成员函数为该类所有对象共享,不仅是处于简化语言实现、节省存储的目的,而且是为了使同类对象有一致的行为。同类对象的行为虽然一致,但是操作不同的数据成员。
 */

posted on 2009-03-11 15:36 pear_li 阅读(1125) 评论(0)  编辑 收藏 引用 所属分类: C++


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理