CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2004
    Posts
    216

    Question overriding a virtual function, question

    Hello, i have a small question.
    I have a base and a derived class.
    In the base class there's a function called f, which is virtual.In the derived class i have overriden the function, and given it the same name.

    If they have different return types, it doesn't compile. but if they have the same return types and different arguments, it does compile. i thought it shouldn't.

    in my base class: virtual int f (int a, int v);
    in my derived class: int f(int b, char c);

    in the stroutstrup book it reads: " A function from a derived class with the same name and the same set of argument types as a virtual function in a base is said to override the base class version of the virtual function".


    There's something i'm not understanding... thanks for your help.

  2. #2
    Join Date
    Jan 2001
    Posts
    588

    Re: overriding a virtual function, question

    In your case, derived::f() is going to hide base::f(). You will not get the polymorphic behavior that you desire. As far as users of your class is concerned, derived does not have a member function with the signature "int f (int a, int v)." In order to override implementations of virtual functions in derived classes, they must have the exact same signature. This is really a symptom of a design problem when you want to reuse the virtual function's name but use different parameters. Perhaps you could give some more information on your application?

  3. #3
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: overriding a virtual function, question

    The ISO standard allows you to change the return type of an override. However, not all compilers allow it.

    Just as a point of terminology: if you say you have "overridden" a function, that means that you have provided a function with the same name and the same argument types (in the same order) - i.e. with the same signature - as the overridden function. So, saying
    In the derived class i have overriden the function, and given it the same name.
    is actually redundant, since "overridden" implies "the same name" (and "the same arguments"). If you write a function with the same name, but different arguments, that's called overloading the function. As Bob Davis correctly points out, if you overload a function in a derived class, you will hide the function in the base class. (So, technically, will overriding a non-virtual function, but the effect is less obvious.) Hope this clears up some of the terminology used in this area (which can be quite confusing at first).
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  4. #4
    Join Date
    Mar 2004
    Posts
    216

    Re: overriding a virtual function, question

    thanks!
    that's just what i thought, i'm killing the polymorphism.
    I'm not working on any project, just studying theory and buinding small progs.

  5. #5
    Join Date
    Apr 2004
    Location
    Canada
    Posts
    1,342

    Re: overriding a virtual function, question

    that's just what i thought, i'm killing the polymorphism.
    I'm not working on any project, just studying theory and buinding small progs.
    I've felt that way about polymorphism before. I suggest that you don't try to use polymorphism in your applications just because everyone says it's one of the most powerful object-oriented capabilities. Wait for a situation when you need polymorphism, and in those situations you'll realize how useful it is.
    Old Unix programmers never die, they just mv to /dev/null

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured