/*
*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指針。
成員函數為該類所有對象共享,不僅是處于簡化語言實現、節省存儲的目的,而且是為了使同類對象有一致的行為。同類對象的行為雖然一致,但是操作不同的數據成員。
*/