Thread: Can some one help to correct my understanding?

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    12

    Can some one help to correct my understanding?

    Hi there, I just found a possibly big mistake in my C++ understanding.

    In below code, BBB:: print overwrites AAA:: print which is virtual, but there is no virtual keyword in BBB. Does this mean calling to print via a BBB pointer is static binding? I supposed so before today, but just now below sample program did not run as I expected.

    It's known to me BBB:: print will overwrite AAA:: print, and my question is, will BBB:: print still keep the virtual attribute so that CCC:: print will also overwite it? I cannot believe I have such a big mis-understanding.


    Thanks for your comments.


    Code:
    #include <stdio.h>
    
    struct AAA
    {
        virtual void print(void)
        {
            printf("%s\n", __PRETTY_FUNCTION__);
        }
    };
    
    struct BBB : public AAA
    {
        void print(void)
        {
            printf("%s\n", __PRETTY_FUNCTION__);
        }
        
        void print(void) const
        {
            printf("%s\n", __PRETTY_FUNCTION__);
        }
    };
    
    struct CCC : public BBB
    {
        void print(void)
        {
            printf("%s\n", __PRETTY_FUNCTION__);
        }
    };

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    A virtual function inherited from a base class is also virtual in a derived class.

    In your example, the non-const version of print() remains virtual in BBB and CCC. The const version is not (and is hidden in CCC).

    Given a pointer to BBB, say p, the call p->print() on a non-const object (i.e. if p is a non-const pointer) will be dynamically bound, and statically bound if p is a const pointer (i.e. points at a const object).

    All static binding means is that the compiler determines what version of the function is called, whereas dynamic binding means the compiler doesn't do that (and the version called is decided at run time).

    Given a pointer to CCC, say c, the call c->print() on a non-const object (i.e. if p is a non-const pointer) will be dynamically bound. If c is a const pointer, the result will be a compiler error (the wonders of the hiding rule).
    Last edited by grumpy; 03-03-2014 at 05:38 AM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    12
    Thanks. It's clear summary.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Which is correct?
    By mramazing in forum C++ Programming
    Replies: 7
    Last Post: 12-29-2010, 02:11 PM
  2. Correct way of doing this?
    By carrotcake1029 in forum C Programming
    Replies: 12
    Last Post: 04-09-2008, 04:26 AM
  3. correct me please
    By ssharish in forum C Programming
    Replies: 3
    Last Post: 03-20-2005, 05:40 AM
  4. Is this correct?
    By thinhare in forum C Programming
    Replies: 14
    Last Post: 12-24-2004, 08:09 PM
  5. is it correct.. ??
    By vivek_kumbhar in forum C Programming
    Replies: 12
    Last Post: 11-02-2002, 05:03 AM