Mike Jiang
Mike Jiang

Reputation: 209

Extend the existing C++ class

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?

Upvotes: 6

Views: 27811

Answers (3)

Freddy
Freddy

Reputation: 2279

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.

Upvotes: 5

Edward Strange
Edward Strange

Reputation: 40859

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.

Upvotes: 3

Dan Korn
Dan Korn

Reputation: 1294

You can't just cast a base class object to an inherited class. Any members in the inherited class will not have been created and initialized. You need to start with an object of the inherited class.

You can go the other way, of course, and call base class functions from a function of a class inherited from the base, because the base class object will always be there as part of the inherited class object, by definition.

If you have a pointer to the base class, and you build with RTTI, you can use dynamic_cast to cast to an inherited class, but that cast will fail and return NULL if the object is not of the class you're trying to cast to. But usually it's better to call a virtual function than to use dynamic_cast.

Upvotes: 0

Related Questions