从子级调用父级的方法,类似于调用"super" (C++)
Call a parent's method from child, similar to calling "super" (C++)
是否可以像Java中的超级运算符一样,在子方法中"重用"父方法并添加功能?
parent.method(int a){
a++;
}
child.method(int a /*would be nice w/out*/){
printf("%d",a);
}
我知道这可能是一个很基本的问题,很抱歉。
我知道我可以通过重载将方法复制/粘贴到子类并在那里添加功能;不过,我正在寻找一种更方便的方式。
您可以使用__super来完成此操作,但它是Microsoft的扩展。
void CChild::function( int nParam )
{
__super::function( nParam );
}
或者,您可以从派生类显式调用基本实现:
void CChild::function( int nParam )
{
CParent::function( nParam );
}
您可以通过用父类的名称限定成员函数名称来调用子成员函数中的父成员函数:
void child::method()
{
parent::method();
}
是的,没有super
关键字,但您只提供父类的名称。
int child::method(int a ){
// call base class
int i = parent::method(a);
printf("%d",a);
}
parent.method(int a){
a++;
}
child::method(int a /*would be nice w/out*/){
printf("%d",a);
parent::method(a);
}
相关文章:
- 什么时候调用组成单元对象的析构函数
- 对RValue对象调用的LValue ref限定成员函数
- 为什么使用 "this" 指针调用派生成员函数?
- 函数调用中参数的顺序重要吗
- OpenGL - 在抛出"__gnu_cxx::recursive_init_error"实例后终止调用?
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 在c++类上调用void函数
- 为什么 std::unique 不调用 std::sort?
- 调用专用模板时出错"no matching function for call to [...]"
- 选择要调用的构造函数
- C++为什么尽管我调用了void函数,它却不起作用
- 构造函数正在调用一个使用当前类类型的函数
- 变量没有改变?通过向量的函数调用
- 没有为自己的结构调用列表推回方法
- 调用'begin(int [n])'没有匹配函数
- 什么时候调用析构函数
- 在UE4中,为什么我们需要覆盖BeginPlay,为什么我们需要在被覆盖的BeginPlay中调用Super::Begi
- 从子级调用父级的方法,类似于调用"super" (C++)
- 通过调用super更改类变量