A question regarding to the keyword "virtual"

Discussion in 'C++' started by wizwx, Mar 23, 2007.

  1. wizwx

    wizwx Guest

    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.
     
    wizwx, Mar 23, 2007
    #1
    1. Advertising

  2. wizwx

    Rolf Magnus Guest

    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'.
     
    Rolf Magnus, Mar 23, 2007
    #2
    1. Advertising

  3. wizwx

    wizwx Guest

    On Mar 23, 8:12 am, Rolf Magnus <> wrote:
    > 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.
     
    wizwx, Mar 23, 2007
    #3
  4. On 23 Mar, 14:32, "wizwx" <> wrote:
    > On Mar 23, 8:12 am, Rolf Magnus <> wrote:
    >
    > > 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
     
    =?iso-8859-1?q?Erik_Wikstr=F6m?=, Mar 23, 2007
    #4
  5. wizwx

    Fei Liu Guest

    wizwx wrote:
    > On Mar 23, 8:12 am, Rolf Magnus <> wrote:
    >> 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
     
    Fei Liu, Mar 23, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dhruba Bandopadhyay

    ASP VIRTUAL keyword not working in ASP.NET anymore :S

    Dhruba Bandopadhyay, Mar 22, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    4,946
    Dhruba Bandopadhyay
    Mar 22, 2006
  2. Shao Zhang
    Replies:
    24
    Views:
    829
    jeffc
    Jul 21, 2004
  3. wizwx
    Replies:
    1
    Views:
    372
    Alexander Block
    Mar 22, 2007
  4. Replies:
    6
    Views:
    478
    Peter Otten
    May 10, 2007
  5. Hamilton, William

    RE: keyword checker - keyword.kwlist

    Hamilton, William, May 10, 2007, in forum: Python
    Replies:
    4
    Views:
    372
Loading...

Share This Page