Thread: A simple question about C++

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    284

    Question A simple question about C++

    I have 2 programs. Why EX1 is correct but EX 2 is not?

    ======EX 1=======
    Code:
    class Base{
    public:
    void foo() const{ }
    
    };
    
    
    class Derived: public Base{
    public:
    int foo() const{ return 1; }
    };
    ======EX 2======

    Code:
    class Base{
    public:
    virtual void foo() const{ }
    
    };
    
    
    class Derived: public Base{
    public:
    int foo() const{ return 1; }
    };

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    In example 1, there are two foo() methods in the Derived class: the one you created which returns an int, and the one that was inherited from Base. Since the signatures are different, you should be able to compile and call those functions fine.

    In example 2, foo() is a virtual method of Base. This means in general that foo() would not be inhereted but overriden explicitly by Derived. Since you changed the signature of foo() in Derived, however, the method isn't necessarily virtual. A v-table can only be constructed if a functions signature remains the same for all derived classes from a base class (which includes those derived classes which rely on the base method).

    So that should have made some sense. It should be covered in a chapter of your C++ textbook if you want to go over it in detail.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Not quite precise.

    In example 1, the functions are distinct: one has the signature Base::foo() and a return type of void (note that return types are not part of the signature), the other has the signature of Derived::foo() and a return type of int. They have nothing to do with each other, other than that Derived::foo hides Base::foo and it is all very confusing to a user of the class.

    In example 2, Base::foo is virtual. Virtual functions are overridden by functions in derived classes that have the same signature, containing class ignored. In other words, for the purpose of overriding, Base::foo has the signature foo(), and Derived::foo also has the signature foo(). Therefore, Derived::foo should override Base::foo.
    The compiler error means that it can't, because the return types differ.

    I think this way of explaining it leads to fewer misunderstandings.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question regarding variables
    By Flakster in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2005, 08:10 PM
  2. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 06:58 PM