C++继承:虚拟成员需要重新声明吗?
我确信这个问题已经以一种或另一种形式被问过,但我找不到线索。请考虑以下 C++ 代码:
// A.h
class A
{
public:
A();
virtual ~A();
virtual void setVisibility(bool v);
virtual bool setVisibility();
protected:
bool visibility;
};
// B.h
class B : public A
{
public:
B();
virtual ~B();
};
// C.h
class C : public B
{
public:
C();
virtual ~C();
virtual void setVisibility(bool v);
virtual bool setVisibility();
};
// A.cpp
#include "common.h"
A::A() {}
A::~A() {}
void A::setVisibility(bool v) { this->visibility = v; }
bool A::setVisibility() { return this->visibility; }
// B.cpp
#include "common.h"
B::B() {}
B::~B() {}
// C.cpp
#include "common.h"
C::C() {}
C::~C() {}
void C::setVisibility(bool v) { /* do nothing */ }
bool C::setVisibility() { return false; }
// common.h - does nothing else than joining the header files together
#include "A.h"
#include "B.h"
#include "C.h"
调用:
C* myC = new C();
cout << "Set visibility true" << endl;
myC->setVisibility(true);
我尝试了几件事。如果我在 C 中虚拟地声明这两个方法,它会编译,但在 myC->setVisibility(true)
处出现分段错误。如果我删除声明(无论如何这都不是必需的,因为它们是从 B 和 A 继承的,对吧?),那么它告诉我 C 没有这些方法。
而且我不想重新实现 B 中的方法。如果我在包括 B 在内的所有地方声明它们,它会告诉我 B 中没有这些方法的实现。
我现在应该做什么?我需要 virtual,因为我并不总是在调用示例中使用 C 作为变量类型。
我在 64 位机器上使用 GCC。
Edit: Corrected the copy/paste-mistake. I named the classes A, B and C for simplicity and didn't copy the code correctly. But unfortunately, the problem remains
Edit 2: Added common.h
Edit 3: Hmm... copying this code actually works fluently. But the architecture is the same. Except that I compile my code into a shared library using the flags
-shared -fPIC
. The calling code is in the application that uses this library. Nothing else is different. Gotta check again.感谢您为我指明正确方向的任何提示。问候
I'm sure this question has already been asked in one form or another, but I couldn't find the clue. Please consider the following C++-code:
// A.h
class A
{
public:
A();
virtual ~A();
virtual void setVisibility(bool v);
virtual bool setVisibility();
protected:
bool visibility;
};
// B.h
class B : public A
{
public:
B();
virtual ~B();
};
// C.h
class C : public B
{
public:
C();
virtual ~C();
virtual void setVisibility(bool v);
virtual bool setVisibility();
};
// A.cpp
#include "common.h"
A::A() {}
A::~A() {}
void A::setVisibility(bool v) { this->visibility = v; }
bool A::setVisibility() { return this->visibility; }
// B.cpp
#include "common.h"
B::B() {}
B::~B() {}
// C.cpp
#include "common.h"
C::C() {}
C::~C() {}
void C::setVisibility(bool v) { /* do nothing */ }
bool C::setVisibility() { return false; }
// common.h - does nothing else than joining the header files together
#include "A.h"
#include "B.h"
#include "C.h"
calling:
C* myC = new C();
cout << "Set visibility true" << endl;
myC->setVisibility(true);
I tried several things. If I declare both methods virtually in C, it compiles but I get a segmentation fault at myC->setVisibility(true)
. If I remove the declaration (which shouldn't be necessary anyway, as they're inherited from B and A, right?), then it tells me that C doesn't have these methods.
And I don't want to reimplement the methods in B. If I declare them everywhere including B, it tells me that there's no implementation of these methods in B.
What should I do now? I need virtual, because I'm not always gonna use C as variable type in the calling example.
I'm using GCC on a 64 bit machine.
Edit: Corrected the copy/paste-mistake. I named the classes A, B and C for simplicity and didn't copy the code correctly. But unfortunately, the problem remains
Edit 2: Added common.h
Edit 3: Hmm... copying this code actually works fluently. But the architecture is the same. Except that I compile my code into a shared library using the flags
-shared -fPIC
. The calling code is in the application that uses this library. Nothing else is different. Gotta check again.Thanks for any tips pointing me to the right direction. regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在C.cpp中,不要定义
A::setVisibility()
,它应该是C::setVisibility()
in C.cpp, don't define
A::setVisibility()
, it should beC::setVisibility()
在
C.cpp
中,您定义的是A::setVisibility
,而不是C::setVisibility
。In
C.cpp
, you're definingA::setVisibility
, notC::setVisibility
.您可以将 C 类更改为:
这将“重新引入”A 中定义的所有类型的 setVisibility。
You can change your class C to:
This will "reintroduce" all kinds of setVisibility defined in A.
更正前其他答案都是正确的。但这不是问题的解决方案。其实根本就没有什么问题。问题出在我的 setVisibility 方法的实现上,这就是段错误发生的地方。因此在这里找不到解决方案。
感谢所有试图提供帮助的人!
问候
The other answers are all correct before the correction. But it's not the solution to the problem. There actually isn't any problem at all. The problem lies in my implementation of the
setVisibility
-method, that's where the segfault occurs. Therefore the solution can't be found here.Thanks to all who tried to help!
regards