Extend the existing C++ class

Mike Jiang picture Mike Jiang · Mar 25, 2016 · Viewed 9.4k times · Source

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?

Answer

Freddy picture Freddy · Mar 25, 2016

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.