C++继承:虚拟成员需要重新声明吗?

发布于 2024-10-16 12:21:27 字数 2032 浏览 1 评论 0原文

我确信这个问题已经以一种或另一种形式被问过,但我找不到线索。请考虑以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

简单 2024-10-23 12:21:27

在C.cpp中,不要定义A::setVisibility(),它应该是C::setVisibility()

in C.cpp, don't define A::setVisibility(), it should be C::setVisibility()

池木 2024-10-23 12:21:27

C.cpp 中,您定义的是 A::setVisibility,而不是 C::setVisibility

In C.cpp, you're defining A::setVisibility, not C::setVisibility.

幸福%小乖 2024-10-23 12:21:27

您可以将 C 类更改为:

// C.h
class C : public B
{
     public:
        C();
        virtual ~C();
        using A::setVisibility;
        virtual bool setVisibility();
};

这将“重新引入”A 中定义的所有类型的 setVisibility。

You can change your class C to:

// C.h
class C : public B
{
     public:
        C();
        virtual ~C();
        using A::setVisibility;
        virtual bool setVisibility();
};

This will "reintroduce" all kinds of setVisibility defined in A.

苏佲洛 2024-10-23 12:21:27

更正前其他答案都是正确的。但这不是问题的解决方案。其实根本就没有什么问题。问题出在我的 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文