virtual functions

Discussion in 'C++' started by puzzlecracker, Jan 16, 2006.

  1. is return type considered in the context of virtual functions?

    ex.

    class Base{

    public:
    virtual int foo(){}
    };

    class Derived:public Base{

    public:
    virtual double foo();

    };

    On par, are access modifier considered, matter, how? in above context:
    let's foo was declared public in Base and in protected in Derived...
    and visa versa........

    I am trying to compare the polymorphism resolution in C++ to Java,
    where in latter there is concept of more/less specific, etc., I always
    *assumed* that return value doesn' play any role whatsoever.

    Please be as detailed as you have time and desire.

    Thanks
    puzzlecracker, Jan 16, 2006
    #1
    1. Advertising

  2. puzzlecracker

    Phlip Guest

    puzzlecracker wrote:

    > is return type considered in the context of virtual functions?


    No, with a narrow exception.

    > virtual int foo(){}
    > virtual double foo();


    The "signature" is everything from the 'foo' going right (with another
    exception of default argument values, which you shouldn't use).

    The compiler should reject the above because the signature is not enough to
    distinguish.

    The exception is "covariant return types". virtual methods may generally
    return similar pointers or references related by inheritance.

    > On par, are access modifier considered, matter, how? in above context:
    > let's foo was declared public in Base and in protected in Derived...
    > and visa versa........


    No, because that would impose a runtime penalty. Recall you can call a
    derived foo from a base class pointer, so if the derived type were private,
    the program might only be able to detect this at runtime. Privacy is only a
    frail compile-time check, and plenty of design patterns depend on public
    interface methods calling private implementations.

    > I am trying to compare the polymorphism resolution in C++ to Java,
    > where in latter there is concept of more/less specific, etc., I always
    > *assumed* that return value doesn' play any role whatsoever.


    Java subtracts much from C++ while adding very little. Research Python,
    Ruby, or Smalltalk to learn how different inheritance systems can be.

    --
    Phlip
    http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
    Phlip, Jan 16, 2006
    #2
    1. Advertising

  3. puzzlecracker

    Ian Collins Guest

    Phlip wrote:
    >
    > Java subtracts much from C++ while adding very little. Research Python,
    > Ruby, or Smalltalk to learn how different inheritance systems can be.
    >

    Or JavaScript :)

    --
    Ian Collins.
    Ian Collins, Jan 16, 2006
    #3
    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. Michael Winter
    Replies:
    9
    Views:
    609
    Michael Winter
    Sep 24, 2003
  2. heted7
    Replies:
    33
    Views:
    1,022
    Chris Dearlove
    May 12, 2005
  3. Replies:
    3
    Views:
    366
    Nitin Motgi
    Jan 31, 2006
  4. Replies:
    7
    Views:
    371
    Richard
    Aug 28, 2006
  5. John Goche
    Replies:
    10
    Views:
    726
    Marcus Kwok
    Dec 8, 2006
Loading...

Share This Page