65,210
社区成员
发帖
与我相关
我的任务
分享
class parent
{
protected:
virtual void tell()
{
cout<<"don't tell anybody"<<endl;
}
};
class child : public parent
{
public:
void tell()
{
cout<<"i want to tell all"<<endl;
}
};
然后你可以类似这样调用
child tempChild;
child.tell();
class parent
{
public:
virtual void tell()
{
cout<<"don't tell anybody"<<endl;
}
};
class child : public parent
{
private:
void tell()
{
cout<<"i want to tell all"<<endl;
}
};
这样上面的那个代码就编译不过了,而且看起来确实没有办法调用tell了,是这样么?
child tempChild;
((parent*)(&tempChild))->tell(); //试试看~ same func as code below
//parent* pParent = &tempChild;
//pParent->tell();
class Test
{
public:
Test(int i):m_i(i){}
void Show(){cout << m_i << endl;}
private:
int m_i;
};
void main()
{
Test t(100);
int *pi = reinterpret_cast<int*>(static_cast<void*>(&t));
*pi = 1;
t.Show();
}
#include <iostream>
using namespace std;
class Base
{
public:
virtual void CutDownRight()
{
cout << "void Base::CutDownPrivate()" << endl;
}
protected:
virtual void ExpandRight()
{
cout << "void Base::ExpandRight();" << endl;
}
};
class Derived : public Base
{
private:
virtual void CutDownRight()
{
cout << "void Derived::CutDownPrivate()" << endl;
}
public:
virtual void ExpandRight()
{
cout << "void Derived::ExpandRight();" << endl;
}
};
void main()
{
Derived* d = new Derived();
//d->CutDownRight(); //wrong
d->ExpandRight(); //ok
Base* bd = new Derived();
bd->CutDownRight(); //ok
//bd->ExpandRight(); //wrong
}