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