/*
*C++中非多態(tài)類對(duì)象的內(nèi)存映像模型
*
*
*
*/
#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;
}
/*
*有如下規(guī)則:
非靜態(tài)數(shù)據(jù)成員被放在每一個(gè)對(duì)象體內(nèi)作為對(duì)象專有的數(shù)據(jù)成員
靜態(tài)數(shù)據(jù)成員被提取出來放在程序的靜態(tài)數(shù)據(jù)區(qū)內(nèi),為該類所有對(duì)象共享,因此只存在一份。
靜態(tài)和非靜態(tài)成員函數(shù)最終都被提取出來放在程序的代碼段中并為該類所有對(duì)象共享,因此每一個(gè)成員函數(shù)也只能存在一份代碼實(shí)體。
因此,構(gòu)成對(duì)象本身的只有數(shù)據(jù),任何成員函數(shù)都不隸屬于任何一個(gè)對(duì)象,非靜態(tài)成員函數(shù)與對(duì)象的關(guān)系就是綁定,綁定的中介就是this指針。
成員函數(shù)為該類所有對(duì)象共享,不僅是處于簡化語言實(shí)現(xiàn)、節(jié)省存儲(chǔ)的目的,而且是為了使同類對(duì)象有一致的行為。同類對(duì)象的行為雖然一致,但是操作不同的數(shù)據(jù)成員。
*/