CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Oct 2001
    Location
    Dublin, Eire
    Posts
    880

    About virtual functions

    Someone gave me a big doubt about virtual functions. Can some one remind me which is the correct way about this:


    Code:
    class Base
    {
      ...
      virtual void display() = 0;
      ...
    };
    
    class Derived : public Base
    {
      ...
      virtual void display();
      ...
    };
    Must we put the keyword "virtual" before the function display in Derived, or must we not put it, or can we do what we want.
    Elrond
    A chess genius is a human being who focuses vast, little-understood mental gifts and labors on an ultimately trivial human enterprise.
    -- George Steiner

  2. #2
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Originally posted by Elrond
    Must we put the keyword "virtual" before the function display in Derived, or must we not put it, or can we do what we want.
    You do not need to repeat the 'virtual' keyword in the derived class, however it will not fail if you do...

  3. #3
    Join Date
    Oct 2001
    Location
    Dublin, Eire
    Posts
    880
    But in this case what is considered as "best practice". To put virtual or not to put it?

    It's easy to check that it will work, but a lot of things work that are not really good practice, and some times not even well defined!
    Elrond
    A chess genius is a human being who focuses vast, little-understood mental gifts and labors on an ultimately trivial human enterprise.
    -- George Steiner

  4. #4
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    I usually repeat the "virtual". It's redundant, but adds a bit of extra documentation, given that the base class is usually defined in a different file. Mind you, I wouldn't bother repeating the "pure" declaration if I intended to have the actual implementation in a further derived class, so maybe I'm inconsistent.
    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


  5. #5
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    I'd like to say that I usually repeat the virtual as well. I've heard
    a lot of people tell you NOT to since it can be confusing ... but I
    find it's more confusing to have to look at one class and then go
    through all of its ancestors to see whether or not the function is
    virtual.

    --Paul

  6. #6
    Join Date
    Oct 2001
    Location
    Dublin, Eire
    Posts
    880
    Someone just gave me part of the standard saying that using the keyword virtual for derived classes is not an error as the derived classes are implicitely virtual. This really means that you can do whatever you want, and that it's just a question of coding practice.

    I used to always repeat the virtual keyword, but someone told me it could be a mistake, so I suddenly had some doubts. This that work with a compiler might still be kind of wrong because the compiler is not standard.

    I guess I'll keep using virtual then.

    Thanks for your help.
    Elrond
    A chess genius is a human being who focuses vast, little-understood mental gifts and labors on an ultimately trivial human enterprise.
    -- George Steiner

  7. #7
    Join Date
    Sep 2002
    Location
    Maryland - Fear The Turtle!
    Posts
    7,537
    Originally posted by PaulWendt
    I'd like to say that I usually repeat the virtual as well. I've heard
    a lot of people tell you NOT to since it can be confusing ... but I
    find it's more confusing to have to look at one class and then go
    through all of its ancestors to see whether or not the function is
    virtual.

    --Paul

    Ditto...

  8. #8
    Join Date
    Mar 2002
    Location
    California
    Posts
    1,582
    This is one of my biggest complaints with C++. I think omitting the virtual keyword should be a compile error, plain and simple.

    This would
    1) prevent you from mistakingly overriding a virtual method, and
    2) make it much easier to determine what is virtual in any given class without searching through the hierarchy.

    Jeff

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