Make the function in base class as pure virtual function, so that will restrict creation of base class object (while creating derived class object) which will eventually avoid object slicing..
By making function as pure virtual in base class, the compiler would prevent object slicing because that wouldn’t allow you to “create” an object of the base type (which is what happens when you upcast by value)
This could be the most important value of pure virtual functions: to prevent object slicing by generating a compile-time error message if someone tries to do it.
No comments:
Post a Comment