類的size & 私有成員的繼承方式

#include<iostream>
using namespace std;

class CParent{
public:
 int m_Variable1;  //size=4 (int)
 virtual void ShowVariable(){            //size=4(虛函數通過一個void pointer  VPTR調用)
  cout<<"CParent::m_Variable1= "<<this->m_Variable1<<endl;
  cout<<"CParent::m_Variable2= "<<this->m_Variable2<<endl;
 };
 void SetVariable2(int x){           //size=0 (編譯後通過內部函數名類似_CParent_SetVariable_int_調用)
  this->m_Variable2=x;
 };
private:
 int m_Variable2;    //size=4  (int)
};

class CMyClass:public CParent{
public:
 void ShowVariable(){
 cout<<"CMyClass::m_Variable1= "<<this->m_Variable1<<endl;
//! cout<<"CMyClass::m_Variable2= "<<this->m_Variable2<<endl;   由上一句的運行結果可知,此時this指的
//的確是CMyClass,CMyClass::m_Variable2在內存中的確存在,但是被限制了不可訪問。故這句會引起編譯錯誤。
 };
};
/* 注意:如果不重載ShowVariable函數的話?此時程序將會調用CParent::ShowVariable(),可將CMyClass中繼承
來的(屬於CParent的私有的)m_Variable2讀出來。但應記住這是錯誤的處理方式*/

void main(){
 cout<<"sizeof CParent is: "<<sizeof(CParent)<<endl;
 cout<<"sizeof CMyClass is: "<<sizeof(CMyClass)<<endl; //private對象未被繼承,比較子類與父類的大小
          //結論:大小相等!說明CMyClass確實繼承了m_Variable2成員
 //私有成員會被繼承,但是不能被訪問。所以看上去他們似乎是不能被繼承的,但實際上確實被繼承了。
 CParent a;
 CMyClass b;
 CParent *pa=&a;
 CMyClass *pb=&b;
 pa->m_Variable1=53;
 pa->SetVariable2(43);
 pb->m_Variable1=56;
 pb->SetVariable2(41); //這一句實際上不應該存在,但是操作上是成立的,可以通過編譯
 cout<<"sizeof pa: "<<sizeof(pa)<<endl;  //與&pa同一個意思,即地址
 cout<<"sizeof &pa: "<<sizeof(&pa)<<endl;
 cout<<"sizeof *pa: "<<sizeof(*pa)<<endl;  //*pa表示pa指向的元素
 cout<<"sizeof pa: "<<sizeof(pb)<<endl;
 cout<<"sizeof &pb: "<<sizeof(&pb)<<endl;
 cout<<"sizeof *pb: "<<sizeof(*pb)<<endl;
 pa->ShowVariable();
 pb->ShowVariable();
 a.ShowVariable();
 b.ShowVariable();
}

//事實上,如果要想在派生的子類中正大光明地訪問基類的私有成員,可以在基類中將子類聲明爲友元
//繼承之後,子類想用父類的東西,就把父類的東西聲明爲protected就行了.
//
//   1、 類的私有區域(用private聲明)只能被類的實現者訪問。
//   2、 類的保護區域(用protected聲明)只能被類的實現者或其派生類訪問。

//對於不是繼承關係的兩個類,一個類想用另一個類的私有,就要友元了.

//訪問控制如private等,是在編譯期控制還是執行期? 是在編譯期。

 

 

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

 

// sizeofclass.cpp : 定義控制檯應用程序的入口點。
//

#include "stdafx.h"


#include<iostream>
using namespace std;

class CParent{
public:
 int m_Variable1;  //size=4 (int)
 virtual void ShowVariable(){            //size=4(void pointer)
  cout<<"CParent::m_Variable1= "<<this->m_Variable1<<endl;
  cout<<"CParent::m_Variable2= "<<this->m_Variable2<<endl;
 };
 void SetVariable2(int x){           //size=0
  this->m_Variable2=x;
 };
private:
 int m_Variable2;    //size=4  (int)
};

class CMyClass:public CParent{
public:
 void ShowVariable(){
 cout<<"CMyClass::m_Variable1= "<<this->m_Variable1<<endl;
//! cout<<"CMyClass::m_Variable2= "<<this->m_Variable2<<endl;
 };
};

void main(){
 cout<<"sizeof CParent is: "<<sizeof(CParent)<<endl; //sizeof(CParent)=12=(4+4+4)
 cout<<"sizeof CMyClass is: "<<sizeof(CMyClass)<<endl; //比較子類與父類的大小
          //結論:大小相等!子類CMyClass實際上繼承了父類私有成員m_Variable2,但是限制其不可訪問。
 CParent *pa=new CParent;
 CMyClass *pb=new CMyClass;
 pa->m_Variable1=53;
 pa->SetVariable2(43);
 pb->m_Variable1=56;
 pb->SetVariable2(41);
 cout<<"sizeof pa: "<<sizeof(pa)<<endl;  //與&pa同一個意思,即地址
 cout<<"sizeof &pa: "<<sizeof(&pa)<<endl;
 cout<<"sizeof *pa: "<<sizeof(*pa)<<endl;  //*pa表示pa指向的元素
 cout<<"sizeof pa: "<<sizeof(pb)<<endl;
 cout<<"sizeof &pb: "<<sizeof(&pb)<<endl;
 cout<<"sizeof *pb: "<<sizeof(*pb)<<endl;
 pa->ShowVariable();
 pb->ShowVariable();
 while(1);
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章