简体   繁体   中英

Code reuse via inheritance and cross-delegation

Assume I have the following class-hierarchy in C++:

class AbstractBaseClass
{
public:
    // Note:  Class is completely abstract! No data!
    AbstractBaseClass() {}
    virtual ~AbstractBaseClass() {}
    virtual std::string definedInVirtual() = 0;
    virtual std::string definedInDerived() = 0;
};

class Virtual : public virtual AbstractBaseClass
{
public:
    Virtual() {}
    virtual ~Virtual() {}

    std::string definedInVirtual()
    {
        return "definedInVirtual";
    }
};

class DerivedA : public Virtual
{
public:
    DerivedA() {}
    virtual ~DerivedA() {}

    std::string definedInDerived()
    {
        return "definedInDerivedA";
    }
};

class DerivedB : public Virtual
{
public:
    DerivedB() {}
    virtual ~DerivedB() {}

    std::string definedInDerived()
    {
        return "definedInDerivedB";
    }
};

In this case, calling definedInVirtual() on either DerivedA or DerivedB , the definition of definedInVirtual() on Virtual is called, via cross-delegation. This is, then, a form of code reuse.

What are the drawbacks to this method? Is there a better way to achieve code reuse in this situation?

EDIT:

The main reason I ask is there was a very large class that someone had developed with the plan of code reuse via inheritance (he since regrets doing this, but the class is large) that was super-classed by a new ABC due to adding a decorator derived from the new ABC to the object hierarchy. I need to add methods to the new ABC that need to be handled by the DerivedA and DerivedB, and for the decorator to handle via calling the internal instance of the ABC. My idea is to use the above technique...

Code re-use should be implemented through composition, not inheritance. Virtual inheritance is slow and extremely messy and should be avoided in nearly all designs.

At the time when I'm writing this answer your question has some undefined and quite uncommon terms, "cross-delegation" and "superclassed" (which makes no sense in C++), and your classes don't make sense: what is the virtual inheritance for?

So, I'm sorry, but about the only thing that can be said as of now is to avoid the ungood things in the presented code:

  • don't let pure value-producing methods be non- const , do add const .
  • what DeadMG noted in his answer, that it's generally a good idea to avoid virtual inheritance.

In your case, at least with what you've shown at this point, the virtual inheritance isn't used for anything at all (your comment about "cross-delegation" might be it, but it doesn't make sense), so it's easy: remove that virtual keyword: it doesn't do anything useful here.

You might try to amend your question with a more relevant example, and without the buzzwords.

(This should ideally have been a comment, but too long for an SO comment. Oops, negativity. Not entirely happy-happy or pseudo-technical positive! Now the anonymous drive-by downvoters will probably be driving by, inspired by "The Weatherman". Oh well.)

Cheers & hth.,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM