Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
361 views
in Technique[技术] by (71.8m points)

oop - C++ best way te determine an inherited object

I have an abstract object inherited by objects with very different needs, but I need to get all of them in the same container at a point.

I have two main ideas, the first :

Define all methods in the abstract object in that way :

class AbstractObject
{
    ...

    virtual bool isObjectSpecial ()
    {
        /* Default behavior : you are not special, so return false. */
        return false;
    };

    virtual void doSomethingSpecial ()
    {
        /* Default behavior : you are really not special, so do nothing.*/
    };

    ...
}

class SpecificObjectA : public AbstractObject
{
    ...

    bool isObjectSpecial () override 
    {
        return true;
    };

    void doSomethingSpecial () override
    {
        /* Do the special A thing ... */
    };

    ...
}

class SpecificObjectB : public AbstractObject
{
    ...

    bool isObjectSpecial () override 
    {
        return true;
    };

    void doSomethingSpecial () override
    {
        /* Do the special B thing ... */
    };

    ...
}

class RegularObjectA : public AbstractObject
{
    ...
}

/* ... and so on. */

And the use case will be :

if ( abstractObjectPtr->isObjectSpecial() )
    abstractObjectPtr->doSomethingSpecial();

The second idea is : Define only common methods to the base abstract and create an other abstract object containing methods for the special case in that way.

class AbstractObject
{
    ...
}

class AbstractSpecialObject : public AbstractObject
{
    ...

    void doSomethingSpecial () = 0; // NOTE: Pure virtual here !

    ...
}

class SpecificObjectA : public AbstractSpecialObject
{
    ...

    void doSomethingSpecial () override
    {
        /* Do the special A thing ... */
    };

    ...
}

class SpecificObjectB : public AbstractSpecialObject
{
    ...

    void doSomethingSpecial () override
    {
        /* Do the special B thing ... */
    };
    
    ...
}

class RegularObjectA : public AbstractObject
{
    ...
}

/* ... and so on. */

And the use case will be :

auto specialAbstractObjectPtr = dynamic_cast< AbstractSpecialObject * >(abstractObjectPtr);

if ( specialAbstractObjectPtr != nullptr )
    specialAbstractObjectPtr->doSomethingSpecial();

From a full OOP perspective, I prefer the second solution, but I don't like the dynamic casting. In the other hand, I don't know what could be wrong with the first one, just the fact it forces the abstract object to be responsible for all methods.

What will you choose and why ? And, did someone have an other solution ?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...