Connecting Tech Pros Worldwide Forums | Help | Site Map

A question regarding to the keyword "virtual"

wizwx
Guest
 
Posts: n/a
#1: Mar 23 '07
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.


Rolf Magnus
Guest
 
Posts: n/a
#2: Mar 23 '07

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'.

wizwx
Guest
 
Posts: n/a
#3: Mar 23 '07

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.

=?iso-8859-1?q?Erik_Wikstr=F6m?=
Guest
 
Posts: n/a
#4: Mar 23 '07

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:
Yes.
>
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

Fei Liu
Guest
 
Posts: n/a
#5: Mar 23 '07

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
Closed Thread