A question regarding to the keyword "virtual" 
March 23rd, 2007, 12:55 PM
| | | A question regarding to the keyword "virtual"
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. | 
March 23rd, 2007, 01:15 PM
| | | Re: A question regarding to the keyword "virtual"
wizwx wrote: Quote:
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. Quote:
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'. | 
March 23rd, 2007, 01:35 PM
| | | Re: A question regarding to the keyword "virtual"
On Mar 23, 8:12 am, Rolf Magnus <ramag...@t-online.dewrote: Quote:
wizwx wrote: Quote:
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.
> Quote:
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. | 
March 23rd, 2007, 01:45 PM
| | | Re: A question regarding to the keyword "virtual"
On 23 Mar, 14:32, "wizwx" <wiz...@gmail.comwrote: Quote:
On Mar 23, 8:12 am, Rolf Magnus <ramag...@t-online.dewrote:
> Quote:
wizwx wrote: Quote:
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.
| | >> Quote: Quote:
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?
| | > Quote:
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 | 
March 23rd, 2007, 02:25 PM
| | | Re: A question regarding to the keyword "virtual"
wizwx wrote: Quote:
On Mar 23, 8:12 am, Rolf Magnus <ramag...@t-online.dewrote: Quote:
>wizwx wrote: Quote:
>>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.
>> Quote:
>>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 | | Thread Tools | Search this Thread | | | |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 220,840 network members.
|