A question regarding to the keyword "virtual"

W

wizwx

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

Rolf Magnus

wizwx said:
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'.
 
W

wizwx

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?=

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

Fei Liu

wizwx said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top