mardi 3 juillet 2018

The fact that the type of function determined in runtime is defined as advantage?

I read that one advantage of using by "inheritance" for generic-code is the fact that the type of the object determined in runtime because that allows more flexibility.

I don't understand this point. How it's really allows more flexibility?


If for example I get object from type Base so that:

class Base{  
public:   
virtual void method() const { /* ... */ }
};

class D1 : public Base{  
public:   
 void method() const override { /* ... */ }
};

class D2 : public Base{  
public:   
 void method() const override { /* ... */ }
};

And I send to function f (for example) that following object:

Base b = new D1;
f(b);

Where is the flexibility (What it's defined as advantage that it's done in runtime) ?

Aucun commentaire:

Enregistrer un commentaire