将叶类重构为基类,并保留它的接口实现

发布于 2024-09-05 17:01:48 字数 2251 浏览 3 评论 0原文

我正在尝试重构工作代码。该代码基本上将接口类派生为工作实现,我想在原始项目之外使用此实现作为独立的类。

但是,我不想创建一个分支,我希望原始项目能够拿出他们的实现,并使用我的。问题是层次结构非常不同,我不确定这是否可行。我也无法在我的项目中使用原始基类,因为实际上它在项目中非常复杂(包含太多类),并且我只需要处理原始项目问题的子域。

我编写这段代码是为了测试如何实现它的想法,虽然它正在工作,但我不确定我喜欢它:

#include <iostream>

// Original code is:
// IBase -> Derived1

// I need to refactor Derive2 to be both indipendet class
// and programmers should also be able to use the interface class
// Derived2 -> MyClass + IBase
// MyClass


class IBase {
public:
    virtual void printMsg() = 0;
};

///////////////////////////////////////////////////
class Derived1 : public IBase {
public:
    virtual void printMsg(){ std::cout << "Hello from Derived 1" << std::endl; }
};


//////////////////////////////////////////////////
class MyClass {
public:
    virtual void printMsg(){ std::cout << "Hello from MyClass" << std::endl; }
};

class Derived2: public IBase, public MyClass{
    virtual void printMsg(){ MyClass::printMsg(); }
};

class Derived3: public MyClass, public IBase{
    virtual void printMsg(){ MyClass::printMsg(); }
};

int main()
{
    IBase *o1 = new Derived1();
    IBase *o2 = new Derived2();
    IBase *o3 = new Derived3();
    MyClass *o4 = new MyClass();

    o1->printMsg();
    o2->printMsg();
    o3->printMsg();
    o4->printMsg();

    return 0;
}

输出按预期工作(使用 gcc 和 clang 进行测试,2 个不同的 C++ 实现,所以我认为我在这里是安全的):

[elcuco@pinky ~/src/googlecode/qtedit4/tools/qtsourceview/qate/tests] ./test1
Hello from Derived 1
Hello from MyClass
Hello from MyClass
Hello from MyClass
[elcuco@pinky ~/src/googlecode/qtedit4/tools/qtsourceview/qate/tests] ./test1.clang 
Hello from Derived 1
Hello from MyClass
Hello from MyClass
Hello from MyClass



问题是

我原来的代码是:

class Derived3: public MyClass, public IBase{
    virtual void IBase::printMsg(){ MyClass::printMsg(); }
};

这就是我想要表达的内容,但这不能编译。我必须承认我并不完全理解为什么这段代码有效,因为我期望新方法 Derived3::printMsg() 将是 MyClass::printMsg() 的实现> 而不是 IBase::printMsg() (即使很难,这也是我想要的)。

当两个“姐妹类”具有相同的虚函数名称时,编译器如何选择重新实现哪个方法?

如果有人有更好的方法来实现这一点,我也想知道:)

I am trying to refactor a working code. The code basically derives an interface class into a working implementation, and I want to use this implementation outside the original project as a standalone class.

However, I do not want to create a fork, and I want the original project to be able to take out their implementation, and use mine. The problem is that the hierarchy structure is very different and I am not sure if this would work. I also cannot use the original base class in my project, since in reality it's quite entangled in the project (too many classes, includes) and I need to take care of only a subdomain of the problems the original project is.

I wrote this code to test an idea how to implement this, and while it's working, I am not sure I like it:

#include <iostream>

// Original code is:
// IBase -> Derived1

// I need to refactor Derive2 to be both indipendet class
// and programmers should also be able to use the interface class
// Derived2 -> MyClass + IBase
// MyClass


class IBase {
public:
    virtual void printMsg() = 0;
};

///////////////////////////////////////////////////
class Derived1 : public IBase {
public:
    virtual void printMsg(){ std::cout << "Hello from Derived 1" << std::endl; }
};


//////////////////////////////////////////////////
class MyClass {
public:
    virtual void printMsg(){ std::cout << "Hello from MyClass" << std::endl; }
};

class Derived2: public IBase, public MyClass{
    virtual void printMsg(){ MyClass::printMsg(); }
};

class Derived3: public MyClass, public IBase{
    virtual void printMsg(){ MyClass::printMsg(); }
};

int main()
{
    IBase *o1 = new Derived1();
    IBase *o2 = new Derived2();
    IBase *o3 = new Derived3();
    MyClass *o4 = new MyClass();

    o1->printMsg();
    o2->printMsg();
    o3->printMsg();
    o4->printMsg();

    return 0;
}

The output is working as expected (tested using gcc and clang, 2 different C++ implementations so I think I am safe here):

[elcuco@pinky ~/src/googlecode/qtedit4/tools/qtsourceview/qate/tests] ./test1
Hello from Derived 1
Hello from MyClass
Hello from MyClass
Hello from MyClass
[elcuco@pinky ~/src/googlecode/qtedit4/tools/qtsourceview/qate/tests] ./test1.clang 
Hello from Derived 1
Hello from MyClass
Hello from MyClass
Hello from MyClass

The question is

My original code was:

class Derived3: public MyClass, public IBase{
    virtual void IBase::printMsg(){ MyClass::printMsg(); }
};

Which is what I want to express, but this does not compile. I must admit I do not fully understand why this code work, as I expect that the new method Derived3::printMsg() will be an implementation of MyClass::printMsg() and not IBase::printMsg() (even tough this is what I do want).

How does the compiler chooses which method to re-implement, when two "sister classes" have the same virtual function name?

If anyone has a better way of implementing this, I would like to know as well :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

べ繥欢鉨o。 2024-09-12 17:01:48

答案是,编译器会覆盖两个函数,如本示例所示:

#include <cstdio>
using std::printf;

class A {
public:
    virtual void a() {
        printf("A::a\n");
    }
};
class B {
public:
    virtual void a() {
        printf("B::a\n");
    }
};
class C : public A, public B {
public:
    virtual void a() {
        printf("C::a\n");
        A::a();
        B::a();
    }
};
int main() {
    C c;
    A &a = c;
    B &b = c;
    printf("Calling C::a via A\n");
    a.a();
    printf("Calling C::a via B\n");
    b.a();
}

输出为:

Calling C::a via A
C::a
A::a
B::a
Calling C::a via B
C::a
A::a
B::a

如果您想覆盖其中一个而不是另一个,则需要重命名它。它不仅会做你想要的,而且也会更清晰。

The answer is, compiler overrides both functions, as can be shown by this sample:

#include <cstdio>
using std::printf;

class A {
public:
    virtual void a() {
        printf("A::a\n");
    }
};
class B {
public:
    virtual void a() {
        printf("B::a\n");
    }
};
class C : public A, public B {
public:
    virtual void a() {
        printf("C::a\n");
        A::a();
        B::a();
    }
};
int main() {
    C c;
    A &a = c;
    B &b = c;
    printf("Calling C::a via A\n");
    a.a();
    printf("Calling C::a via B\n");
    b.a();
}

The output is:

Calling C::a via A
C::a
A::a
B::a
Calling C::a via B
C::a
A::a
B::a

If you want to override one and not the other, you need to rename it. Not only it will do what you want, but it will be clearer as well.

星星的轨迹 2024-09-12 17:01:48

编译器实际上并不选择要重写的两种方法之一。相反,它发现有两个具有相同签名的虚拟函数,并在派生类中查找该签名的重写。

如果 Derived3 没有重新实现 printMsg,那么在 Derived3 类型的对象上调用该成员将是不明确的。由于您已经重新实现了该方法,编译器可以愉快且明确地在 Derived3 类型的对象上调用该版本。

The compiler doesn't actually choose one of the two methods to override. Instead it spots that there are two virtual functions with identical signatures and looks for an override for that signature in derived classes.

If Derived3 had not re-implemented printMsg then calling that member on an object of type Derived3 would have been ambiguous. Since you have re-implemented the method the compiler can happily and unambiguously call that version on objects of type Derived3.

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