Extend the existing C++ class

11,982

Solution 1

The way you're extending Base you're not making use of the vtable because you have no virtual methods. It may be easier to think of it as Derived has A Base; That you created a new class that contains a Base member variable.

My Suggestion.

Template Function

I personally would go with a template function. You can keep all the work in your original question, and avoid the need of adding virtual calls to your class.

template<typename T>
void serialize_object(T& t)
{
  t.serialize()
}

And then based on your example.

Derivied d;
serialize_object(d);

The big benefit is that you're not adding runtime cast here. The compiler will inform you if you pass an object that doesn't have a method serialize.

Go Virtual

If you really want to handle this through a virtual interface do so.

struct Serializable{
 virtual void serialize()=0;
 virtual ~Serializable(){}
}

class Derived : public Serializable {
  public:
   void serialize() override;
}

void Derivied::serialize()
{
 std::cout << "Yah\n";
}

Then in your code.

Derivied d;
Serializable& s = dynamic_cast<Serializable&>(d);

However, the big concern here is who is the owner of your base class? Did they provide a virtual dtor? If not, then making use of std::unique_ptr or std::shared_ptr could cause you to not deal directly with the interface.

Solution 2

If you can write the serialize function in a derived class without touching private or protected members then you should simply make it a free function. That solves everything.

Share:
11,982
Mike Jiang
Author by

Mike Jiang

Updated on June 11, 2022

Comments

  • Mike Jiang
    Mike Jiang about 2 years

    I'd like to add the extra functionality without changing the existing class.

    Say,

    class base{
    public:
        int i;
        base(){i = 1;}
        virtual void do_work(){ /*Do some work*/ }
    };
    

    If I want to add serialization member function to it, I will simply create a derived class

    class derived : public base{
    public:
        void serialize();
    };
    
    void derived::serialize(){
        cout << "derived class" << endl;
    }
    

    And I do need to handle existing base objects,e.g.

    int main(){
        base a;
        derived & b = static_cast<derived &>(a);
    
        b.serialize();
    }
    

    This example runs without problems. But I do know the downcast through static_cast is something to be avoided in general.

    But I'd like to know if the downcast for this particular use case can be considered safe since the derived class only has one extra member function. Will it has some potential undefined behavior for accessing vtable?