我在C++中有这个父类
//ParentClass header file
public ParentClass{
public:
ParentClass();
virtual void someParentFunction();
private:
//other member variables and functions
};
//Functions implemented done in respective .cpp file
Run Code Online (Sandbox Code Playgroud)
我扩展了这个课程,所以我有一个看起来像这样的孩子
//ChildOneClass header file
public ChildOneClass : public ParentClass{
public:
//Constructors and other functions
private:
//Other members
};
//Functions implemented in respective .cpp file
Run Code Online (Sandbox Code Playgroud)
示例声明:
//Dynamically create one ChildOneClass object
ChildOneClass * c = new ChildOneClass();
//I know this is never done, but for example purposes i just did this
void * v = c;
Run Code Online (Sandbox Code Playgroud)
我知道如果你有一个指向对象的指针,你可以做到这两点:
((ParentClass *) v)->someParentFunction();
Run Code Online (Sandbox Code Playgroud)
要么:
((ChildOneClass *) v)->someParentFunction();
Run Code Online (Sandbox Code Playgroud)
但哪种方式是正确的方法?如果我将指向子类的指针转换为父类是否重要?很抱歉,如果这令人困惑,如果问题令人困惑,请给我一些反馈意见.我会尽力澄清
| 归档时间: |
|
| 查看次数: |
232 次 |
| 最近记录: |