类的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);
}

 

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