Virtual Functions Please???
Friends ,
I have a question about virtual functions.
The basic idea behind them is that they help us provide a mechanism to redine(or Override a definition) them in the derived classes.And also a constant interface(that is we can access the correct redefinition of the function depending upon the object assigned to the base class pointer.Also called polymorphism).
All this comes at the cost associated with Late or Dynamic Binding and the space needed to maintain the VTABLES.
So then if we need to redefine the functions , then why not add standalone functions each time we need a new redefinition of the function.And add separate functions calls(as the redefinied functions will have a different name).
Each time we realize that a class should provide some redefinition , then instead of declaring the function as a Virtual function in the base class and then provide an overriding redefinition , We , On the contrary , Add our standalone function and also a function call to it .
NO dynamic binding AND no extra space consumption due to Vtables.
Kindly Help
Thanks in advance Matu
:D
Re: Virtual Functions Please???
Quote:
Originally posted by matu So then if we need to redefine the functions , then why not add standalone functions each time we need a new redefinition of the function.And add separate functions calls(as the redefinied functions will have a different name).
Here is where your idea is not going to work:
Code:
class Base
{
public:
// A function that performs a set of tasks in a specific order.
void DoTasks() { Initialize(); Start(); Cleanup(); }
virtual void Initialize() { }
virtual void Start() { }
virtual void Cleanup() { }
virtual ~Base() { }
};
class Derived : public Base
{
public:
// These will never be called!
void DerivedInitialize( );
void DerivedStart();
void DerivedCleanup();
// These will be called
void Initialize( );
void Start();
void Cleanup();
};
The base class has a function called DoTask() that performs the Initialize, Start, and Cleanup function, in that order. The Initialize, Start, and Cleanup functions *must* be done in that order, and DoTask enforces this rule. In a real-world environment, you have no control over Base's interface or rules -- you can't just pull out functions out of Base and replace them with your own.
Given the above scenario, when DoTask is called, how do you propose to add your own function so that DoTask calls it correctly? You have no choice but to provide implementations for the virtual functions. You can't create new member functions in your derived class called DerivedInitialized, DerivedStart, and DerivedCleanup, since none of them will ever get called. Add to that the possibility that the implementation of Base might be in an external object file and may not be inline (like the example above), so you have no access to the source code to Base. You have to use the implementation as-is.
Also, if you plan on deriving from a base class, your destructor should be virtual if at least one function is virtual, therefore the v-table must exist. I will also state that a class should not be derived from if it doesn't have a virtual destructor. If you do derive from such a class, you must be very careful in how the derived object is used.
But in general, if you have none of these (virtual functions, virtual destructor) in your base class, then it shouldn't be a base class, but a standalone class that should not be derived from. The STL is a perfect example of classes that do not have virtual functions or virtual destructors, and they are specifically not meant to be derived from because of these missing pieces. Instead, these classes are used as building blocks (i.e. members) within other classes. So the class you are now using should not be derived from (you can use it, but you will be in trouble if your code delete's a dynamically allocated derived object through the base class pointer).
Quote:
We , On the contrary...
Who is "we"?
Regards,
Paul McKenzie