由买买提看人间百态

topics

全部话题 - 话题: cbase
(共0页)
n*****u
发帖数: 198
1
来自主题: Programming版 - 问一个问题。。
class CBASE{
protected:
int a;
public:
int b;
xxxxx
};
CDERIVED1, CDERIVED2都从CBASE继承过来
有一个function希望对所有的CBASE的b操作,
void function( CBASE* p , int size){
for(int k=0; k p[k].b = xxxxxx;
}
}
main()
{
CBASE *p0 = new CBASE[size0];
CBASE *p1 = new CDERIVED1[size1];
CBASE *p2 = new CDERIVED2[size2];

function ( p0,size0);
function ( p1,size1);
function (p2,size2);
只有第一个call是正确的,后面两个因为derived class又定义了其他成员,用CBASE*
的pointer好像会出错。。。有什么办法可以让fu
g***l
发帖数: 2753
2
指向子类对象的基类指针能访问子类中重载的虚函数,而不能访问其私有函数。
比如
class CBase
{
public:
CBase();
virtual void Print(){cout<<"base"< };
class CDerived: public CBase
{
public:
CDerived();
virtual void Print(){cout<<"Derived"< void Personal(){cout<<"Derived private owned"< }
1. int main()
2. {
3. CDerived oderived;
4. CBase* pbase=&oderived;
5. pbase->Print();
6. pbase->Personal();
7. }
编译的时间,error: ‘class CBase’ has no member named ‘Personal’in line 6.
我的问题是,
1. 如果是因为pbas... 阅读全帖
(共0页)