Thread: strange virtual function output

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    strange virtual function output

    Hello everyone,


    Why in the below code,

    pb->foo();

    will output Final other than output Base? I have tested that the output is Final.

    My question is
    1. pb points Final instance, and the foo in Final is not virtual method;
    2. if function is virtual, we should invoke the foo based on the type of instance pointed to, if not virtual, we should invoke the foo based on the type which is the pionter type;
    3. since foo in Final is not final, so the output should be invoking the foo based on the type of pointer (which is Base), then the output should be Base?

    Code:
    #include <iostream>
    
    using namespace std;
     
    class Base {
    public:
        virtual int foo() {cout << "Base" << endl; return 0;}
    };
     
    class Derived: public Base
    {
    public:
        int foo() {cout << "Derived" << endl;return 0;}
    };
     
    class Final: public Derived{
    public:
      int foo() {cout << "Final" << endl;return 0;}
    };
     
    int main()
    {
        Final f;
        Base* pb = reinterpret_cast<Base*> (&f);
        pb->foo();
     
        return 0;
    }

    regards,
    George

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    You probably wanted:

    Base* pb = &f;
    Last edited by Mario F.; 02-08-2008 at 07:30 AM. Reason: shameful lack of &
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    http://msdn2.microsoft.com/en-us/lib...73(VS.60).aspx
    The virtual keyword is needed only in the base class's declaration of the function; any subsequent declarations in derived classes are virtual by default.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    As vart says: Once you make a function virtual, it will be virtual in all derived classes whether you specify it virtual or not. It is (in my opinion) nice to specify virtual in the derived class, just so that you don't have to work back to the original base class to find out if the function is virtual or not, but it's not necessary from the compilers perspective.

    Note however that if you change the signature of the function (e.g. change argument count, argument type or return type), this function will:
    1. Hide the original function.
    2. Not be virtual unless you specify it.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks vart and Mats,


    My question is answered.

    Quote Originally Posted by matsp View Post
    As vart says: Once you make a function virtual, it will be virtual in all derived classes whether you specify it virtual or not. It is (in my opinion) nice to specify virtual in the derived class, just so that you don't have to work back to the original base class to find out if the function is virtual or not, but it's not necessary from the compilers perspective.

    Note however that if you change the signature of the function (e.g. change argument count, argument type or return type), this function will:
    1. Hide the original function.
    2. Not be virtual unless you specify it.

    --
    Mats

    regards,
    George

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-28-2009, 09:25 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 09:10 AM
  4. Base converter libary
    By cdonlan in forum C++ Programming
    Replies: 22
    Last Post: 05-15-2005, 01:11 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM