在 C++ 中扩展抽象类的混乱;
在我的 Class1
头文件中,我有:
class Class1 : Class2::Callback {
public:
//Class2::Callback method
virtual bool class2Method(int i);
}
在 Class1.cxx
中,我有:
bool Class1::class2Method(int i) {
if (i == 1) return true;
return false;
}
在另一个地方,我有:
IWantAClass2Callback((Class2::Callback)instanceOfClass1);
当我尝试编译时,出现以下错误:
MyApp.cxx In constructor 'MyApp()':
MyApp.cxx:55:55: error: 'Class2::Callback is an inaccessible base of Class1'
MyApp.cxx:55:55: error: cannot allocate an object of abstract type 'Class2::Callback'
Class2.h:16:10: note: because the following virtual functions are pure within 'Class2::Callback'
Class2.h:19:18: note: virtual bool Class2::Callback::class2Method(int)
我是什么做错了吗?
以下是 Class2::Callback
的定义,如 Class2.h
中所定义:
class Class2
{
public:
struct Callback {
virtual bool class2Method(int i) = 0;
};
}
In my header file for Class1
I have:
class Class1 : Class2::Callback {
public:
//Class2::Callback method
virtual bool class2Method(int i);
}
in Class1.cxx
I have:
bool Class1::class2Method(int i) {
if (i == 1) return true;
return false;
}
In another place I have:
IWantAClass2Callback((Class2::Callback)instanceOfClass1);
When I try and compile I get the following error:
MyApp.cxx In constructor 'MyApp()':
MyApp.cxx:55:55: error: 'Class2::Callback is an inaccessible base of Class1'
MyApp.cxx:55:55: error: cannot allocate an object of abstract type 'Class2::Callback'
Class2.h:16:10: note: because the following virtual functions are pure within 'Class2::Callback'
Class2.h:19:18: note: virtual bool Class2::Callback::class2Method(int)
What am I doing wrong?
Here's the definition of Class2::Callback
as defined in Class2.h
:
class Class2
{
public:
struct Callback {
virtual bool class2Method(int i) = 0;
};
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您在此处使用了私有继承。
Class1
的声明应如下所示:You have used private inheritance here. The declaration of
Class1
should look like this:在 C++ 中,类的默认访问说明符是 private:
应该可以解决问题。或者使其成为默认访问说明符为 public 的结构。
In c++ the default access specifier for classes is private:
Should do the trick. That or make it a struct whose default access specifier is public.
您的代码中有两个问题:
正如许多人指出的那样,您使用了私有继承而不是公共继承
一。
转换为
Class2::Callback
时使用切片:(Class2::Callback)instanceOfClass1
。要使用多态性,您需要一个指针或引用。假设您IWantAClass2Callback
具有以下签名:void IWantAClass2Callback(Class2::Callback&);
(返回类型无关紧要),正确的调用是:
IWantAClass2Callback(instanceOfClass1);
There're two problems in your code:
As many pointed out, you used private inheritance instead of public
one.
You use slicing when converting to
Class2::Callback
:(Class2::Callback)instanceOfClass1
. To use polimorphism you need a pointer or a reference. Assuming that youIWantAClass2Callback
has following signature:void IWantAClass2Callback(Class2::Callback&);
(return type doesn't matter), correct call would be:
IWantAClass2Callback(instanceOfClass1);
回调必须驻留在
Class2
的public:
区域,是这样吗?我是说:The Callback must reside in
public:
area ofClass2
, is that the case? I mean:真的有吗
你里面 ?你期望它做什么?您可以转换指针和引用,但不能转换实例。
尝试将签名更改为
并调用
Do you really have
in there? what do you expect it to do? you can cast pointers and references, but no instances.
try changing the signature to
and calling