Pointer to a unified interface - points to a class that implements one and extends from the implementation of another

I have two interfaces:

class FirstInterface
{
    virtual int getId() const = 0;
};

class SecondInterface
{
    virtual void setId(int id) = 0;
};

This is a combined interface:

class CombinedInterface : public FirstInterface, public SecondInterface
{

};

This is the concrete class of the first interface:

class FirstConcrete : public FirstInterface
{
    virtual int getId() const
    {
        return 1;
    }
};

This CompleteConcrete class should now have a CombinedInterface , but at the same time, you must reuse the FirstConcrete implementation .

class CompleteConcrete : public FirstConcrete, public SecondInterface
{
    virtual void setId(int id) { }
};

// This is wrong C++
// Cannot convert from CompleteConcrete * to CombinedInterface *
// CombinedInterface * combinedInterface = new CompleteConcrete();

This does not work, of course. Does anyone know a way to achieve this in C ++?

+4
source share
2 answers

Here's a virtual inheritance-based solution that I mentioned in the comments:

class FirstInterface
{
    virtual int getId() const = 0;
};

class SecondInterface
{
    virtual void setId(int id) = 0;
};


class CombinedInterface : virtual public FirstInterface,
              virtual public SecondInterface
{

};

class FirstConcrete : virtual public FirstInterface
{
    virtual int getId() const
    {
        return 1;
    }
};

class CompleteConcrete : virtual public FirstConcrete,
             virtual public CombinedInterface
{
    virtual void setId(int id) { }
};

void example()
{
    CombinedInterface * combinedInterface = new CompleteConcrete();
}

, ( ), , CompleteConcrete CombinedInterface SecondInterface. : CompleteConcreate CombinedInterface, SecondInterface.

. . ++, , TMK. , -. :

  • , .

  • , , , , -, .

, , - .

P.S. , . , , CombinedInterface , - , :

 void somefunction(CombinedInterface &object);

.

:

 void somefunction(FirstInterface &first, SecondInterface &second);

, . CompleteConcrete, , - . , , - :

template<typename T> void somefunction(T &&t)
{
    real_somefunction(std::forward<T>(t), std::forward<T>(t));
}

void real_somefunction(FirstInterface &first, SecondInterface &second);

CombinedInterface , somefunction(), real_somefunction() .

, , ?

class combined_pointer : public std::pair<FirstInterface *, SecondInterface *> {

public:
    template<typename T> combined_pointer(T *t)
            : std::pair<FirstInterface *, SecondInterface *>(t, t)
    {}
};

.

+1

.

, , .

class FirstInterface
{
    virtual int getId() const = 0;
};

class SecondInterface
{
    virtual void setId(int id) = 0;
};

class CombinedInterface : virtual public FirstInterface, public SecondInterface
{

};

class FirstConcrete : virtual public FirstInterface
{
    virtual int getId() const
    {
        return 1;
    }
};


class CompleteConcrete : public CombinedInterface, public FirstConcrete
{
    virtual void setId(int id) { }
};


// warning: C4250:  inherits via dominance
CombinedInterface * combinedInterface = new CompleteConcrete();
0

Source: https://habr.com/ru/post/1665420/


All Articles