C++ C++;在派生类和基类中使用两个同名变量

C++ C++;在派生类和基类中使用两个同名变量,c++,class,inheritance,casting,polymorphism,C++,Class,Inheritance,Casting,Polymorphism,我之前已经发布过关于这个问题的帖子(),这是一种不同的解决方法。这个解决方案似乎更好地封装了那些实现类的人的行为,因为它防止了他们需要显式地向上转换 问题是: 我有一个项目,我想在大多数对象中隔离核心行为,同时通过派生对象提供附加行为。很简单: class BaseA { virtual void something() {} } class DerivedA : public BaseA { void something() {} void somethingEls

我之前已经发布过关于这个问题的帖子(),这是一种不同的解决方法。这个解决方案似乎更好地封装了那些实现类的人的行为,因为它防止了他们需要显式地向上转换

问题是:

我有一个项目,我想在大多数对象中隔离核心行为,同时通过派生对象提供附加行为。很简单:

class BaseA
{
    virtual void something() {}
}


class DerivedA : public BaseA
{
    void something() {}
    void somethingElse() {}
}
现在假设我还有第二组类,相同的继承方案,只是它们聚合了上面的类。但是,我希望基本版本使用基类,而派生版本使用派生类。我的解决方案是使用相同的名称“隐藏”基类变量

class BaseB
{
    BaseA *var;

    BaseB()
    {
        var = new BaseA();
    }

    virtual void anotherThing1();
    virtual void anotherThing2();
    virtual void anotherThing3();
}

class DerivedB : public BaseB
{
    DerivedA *var;

    DerivedB()
    {
        var = new DerivedA();
    }

    void anotherThing1();
    void anotherThing2();
    void anotherThing3();
    void andAnother1();
    void andAnother2();
}
这种方法的目标是使依赖派生聚合类的函数不再需要显式强制转换来实现获得的功能

void func1( BaseB &b )
{
    b.anotherThing1();
    b.var->something();
}

void func2( DerivedB &b )
{
    b.anotherThing1();
    b.andAnother1();
    b.var->something();
    b.var->somethingElse();
}

void main( int argc, char **argv )
{
    BaseB    baseB;
    DerivedB derivedB;

    func1( baseB );
    func1( derivedB );
    func2( derivedB );
}
这会被认为是坏习惯吗

这会被认为是坏习惯吗

是的,这将是一种不好的做法,因为
Base
中的
var
将不被使用。它看起来不像
DerivedB
应该派生自
BaseB
:相反,它们应该派生自相同的抽象基类,如下所示:

class AbstractB {
public:
    virtual void anotherThing1() = 0;
    virtual void anotherThing2() = 0;
    virtual void anotherThing3() = 0;
};
class DerivedB1 : public AbstractB { // Former BaseB
    BaseA *var;

public:
    DerivedB1() {
        var = new BaseA();
    }
    virtual void anotherThing1();
    virtual void anotherThing2();
    virtual void anotherThing3();
};
class DerivedB2 : public AbstractB { // Former DerivedB
    DerivedA *var;
public:
    DerivedB2() {
        var = new DerivedA();
    }
    void anotherThing1();
    void anotherThing2();
    void anotherThing3();
    void andAnother1();
    void andAnother2();
};

这里使用的一般原则是,您应该尝试将继承层次结构中的所有非叶类抽象化。

为什么要这样做?一个
Base*var
对他们两个都有好处吗?如果不是的话,为什么你希望他们是同一个名字-这是故意阻止别人能够阅读代码或其他东西吗?我只是想得到这个方法的输入。我最初想到只使用一个Base*var(我文章开头的链接)。但是我想知道对于那些实现类的人来说,这是否会更容易,因为他们永远不需要显式地为派生功能强制转换是的,我希望不要使用这种模式,因为派生类中几乎所有被重写的函数都只调用基版本,然后运行一些额外的行。因此,通过完全分离类,将有大量代码重复。@Pondwater您可以将共享功能移动到一个抽象类中,派生类提供对逻辑的特定调整。查看模式(与C++模板无关)。共享功能依赖于聚合对象。因此,在我的例子中,另一个东西#()需要var属性。DerivedB2要求var为DerivedA。@Pondwater您可以对
BaseA
/
DerivedA
对应用相同的重构:使用虚拟函数使
DerivedB1
DerivedB2
仅在
BaseA
的基本功能方面工作(即,调用将是相同的,但由于多态性,实际函数将基于运行时类型)。这应该统一
DerivedB1
/
DerivedB2
代码的部分。将不可统一的代码放入
DerivedB1
/
DerivedB2
虚拟现实中,并从抽象库调用它们以完成模板方法实现。