By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,305 Members | 1,764 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,305 IT Pros & Developers. It's quick & easy.

Virtual Function override

P: 10
I am still a beginner in C++:

Lets say i need to derive a class D from a base class B. And I have a need to override B's function called vulnerable()

There are two ways I can do it:

1. Normal override - Just re-define the function in class D
2. Virtual Function Override - Delcare it as virtual in B and then override in B

What is the advantage of one over the other?
Oct 4 '06 #1
Share this Question
Share on Google+
5 Replies


Banfa
Expert Mod 5K+
P: 8,916
I am still a beginner in C++:

Lets say i need to derive a class D from a base class B. And I have a need to override B's function called vulnerable()

There are two ways I can do it:

1. Normal override - Just re-define the function in class D
2. Virtual Function Override - Delcare it as virtual in B and then override in D

What is the advantage of one over the other?
The difference comes when you call the function via the base class. In 1 if you call the function via the base class the function in the base class is called, in 2 if you call the function via the base class the function in the derived class is called.

And that is basically it.
Oct 4 '06 #2

P: 10
The difference comes when you call the function via the base class. In 1 if you call the function via the base class the function in the base class is called, in 2 if you call the function via the base class the function in the derived class is called.

And that is basically it.
But what if I have multiple derived classes which override the function, how will 2 work?
Oct 4 '06 #3

Banfa
Expert Mod 5K+
P: 8,916
If you have C derived from B derived from A

And if the is a function in all classes fn that is not virtual and a function in all classes vfn that is virtual (in all classes)

If you have a single instance of class C ic then

If you call fn in the context of B b::fn will be called, if you call fn in the context of A A::fn will be called.

If you call vfn in the context of B C::vfn will be called if you call vfn in the context of A C::vfn will be called

If you have a single instance of class B ib then

If you call fn in the context of B b::fn will be called, if you call fn in the context of A A::fn will be called. (you can't call fn in the context of C)

If you call vfn in the context of B B::vfn will be called if you call vfn in the context of A B::vfn will be called


An instance of a class includes a virtual function table which points to any virtual function overrides for the class so that no matter what level of base class calls the virtual function the function in the actual class instance is called.
Oct 5 '06 #4

P: 10
Thanks! But sorry, the more I read about virtual functions, the more confused I am

Lets take this example:
class Window // Base class for C++ virtual function example
{
public:
virtual void Create() // virtual function for C++ virtual function example
{
cout <<"Base class Window"<<endl;
}
};

class CommandButton : public Window
{
public:
void Create()
{
cout<<"Derived class Command Button - Overridden C++ virtual function"<<endl;
}
};

void main()
{
Window *x, *y;

x = new Window();
x->Create();

y = new CommandButton();
y->Create();
}


In function main, for x->Create(), we are calling Create() in the context of x and since Create is a virtual Function re-defined in CommandButton class, is the output "Derived class Command Button - Overridden C++ virtual function" ?
Can you please explain?
Oct 10 '06 #5

P: 14
base pointer can point to derive instant, but derive pointer can not point to base instant
eg: base * bptr = new derive( ); // valid
derive * dptr = new base( ); // invalid

you can write these ways:

base * bptr = new bptr( );
base * bptr = new dptr( );
derive * dptr = new dptr( );

you have these inheritance as following:

class base {
public:
base ( ) { }
void iFunction( ){
.....
}
}

class derive : base {
public:
derive ( ) { }
void iFunction( ){
......
}
}

void main( ) {
base * bptr = new base( );
bptr->iFunction( ); // base::iFunction is called

///////////////////////////////////////////////////////
bptr = new derive( );
bptr->iFunction( ); // base::iFunction is called
///////////////////////////////////////////////////////

derive * dptr = new derive( );
dptr->iFunction( ); // derive::iFunction is called
}

but if you define virtual void iFunction( ) in your base class

/////////////////////////////////////////////////////
bptr = new derive( );
bptr->iFunction( ); // base::iFunction is called
////////////////////////////////////////////////////

so, virtual is used for above case.

bptr = new base( );
dptr = new derive( );

these above case have not need virtual.
Oct 11 '06 #6

Post your reply

Sign in to post your reply or Sign up for a free account.