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

A question regarding to the keyword "virtual"

P: n/a
The followings are a few lines of code from the Template method in
"Thinking in C++" vol.2 pp.639

class ApplicationFramework {
protected:
virtual void customize1() = 0;
virtual void customize2() = 0;
public:
void templateMethod() {
customize1();
customize2();
}
};

class MyApp : public ApplicationFramework {
protected:
void customize1() { cout << "Hello " ; }
void customize2() { cout << "World!\n"; }
};

int main() {
MyApp app;
App.templateMethod();
}

The output will be
Hello World!

However, if I remove the keyword "virtual" in the class
ApplicationFramework and provide a definition for customize1 and
customize2, then the customize1 and customize2 in the class
ApplicationFramework are called, instead of the ones defined in the
class MyApp.

It seems that dynamic binding must come to play at some point. But I
don't see why this is so. Can anyone give me some helpful insight?
Thanks.

Mar 23 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
wizwx wrote:
However, if I remove the keyword "virtual" in the class
ApplicationFramework and provide a definition for customize1 and
customize2, then the customize1 and customize2 in the class
ApplicationFramework are called, instead of the ones defined in the
class MyApp.
Yes.
It seems that dynamic binding must come to play at some point. But I
don't see why this is so. Can anyone give me some helpful insight?
Well, this is the sole purpose of the 'virtual' keyword. It means 'activate
dynamic binding'.

Mar 23 '07 #2

P: n/a
On Mar 23, 8:12 am, Rolf Magnus <ramag...@t-online.dewrote:
wizwx wrote:
However, if I remove the keyword "virtual" in the class
ApplicationFramework and provide a definition for customize1 and
customize2, then the customize1 and customize2 in the class
ApplicationFramework are called, instead of the ones defined in the
class MyApp.

Yes.
It seems that dynamic binding must come to play at some point. But I
don't see why this is so. Can anyone give me some helpful insight?

Well, this is the sole purpose of the 'virtual' keyword. It means 'activate
dynamic binding'.
Can you explain more on this? Well I agree that virtual keyword
enables dynamic binding. The typical use of virtual function is that
you call it through a base pointer/reference that actually refers to
the derived class. But in this example I don't see any pointer or
reference, that's why it confuses me.

Mar 23 '07 #3

P: n/a
On 23 Mar, 14:32, "wizwx" <wiz...@gmail.comwrote:
On Mar 23, 8:12 am, Rolf Magnus <ramag...@t-online.dewrote:
wizwx wrote:
However, if I remove the keyword "virtual" in the class
ApplicationFramework and provide a definition for customize1 and
customize2, then the customize1 and customize2 in the class
ApplicationFramework are called, instead of the ones defined in the
class MyApp.
Yes.
It seems that dynamic binding must come to play at some point. But I
don't see why this is so. Can anyone give me some helpful insight?
Well, this is the sole purpose of the 'virtual' keyword. It means 'activate
dynamic binding'.

Can you explain more on this? Well I agree that virtual keyword
enables dynamic binding. The typical use of virtual function is that
you call it through a base pointer/reference that actually refers to
the derived class. But in this example I don't see any pointer or
reference, that's why it confuses me.
It's because templateMethod() is declared in ApplicationFramework, if
the methods are not virtual it will call those functions local to the
class, but since you declared them virtual it has to take a look at
the vtable to find the method to call.

--
Erik Wikström

Mar 23 '07 #4

P: n/a
wizwx wrote:
On Mar 23, 8:12 am, Rolf Magnus <ramag...@t-online.dewrote:
>wizwx wrote:
>>However, if I remove the keyword "virtual" in the class
ApplicationFramework and provide a definition for customize1 and
customize2, then the customize1 and customize2 in the class
ApplicationFramework are called, instead of the ones defined in the
class MyApp.
Yes.
>>It seems that dynamic binding must come to play at some point. But I
don't see why this is so. Can anyone give me some helpful insight?
Well, this is the sole purpose of the 'virtual' keyword. It means 'activate
dynamic binding'.

Can you explain more on this? Well I agree that virtual keyword
enables dynamic binding. The typical use of virtual function is that
you call it through a base pointer/reference that actually refers to
the derived class. But in this example I don't see any pointer or
reference, that's why it confuses me.
When you remove virtual, the compiler uses static resolution (binding).
You are confusing pointer with dynamic binding. pointer use in
polymorphic code is a facility not a cause.

Fei
Mar 23 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.