CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15
  1. #1
    Join Date
    Apr 2001
    Posts
    1,029

    virtual method question

    Hello,

    Does a method need to be virtual to be over-ridden in a subclass? For example:

    class A
    {
    public:
    A();
    ~A();
    virtual void DoSomething();
    }

    class B : public A
    {
    public:
    B();
    ~B();
    void DoSomething();
    }

    Do I have to have the "virtual" keyword in there? What benefit does it provide if it is not a pure virtual method?

    Thanks!

  2. #2
    Join Date
    May 2009
    Posts
    2,413

    Re: virtual method question

    Quote Originally Posted by lab1 View Post
    Does a method need to be virtual to be over-ridden in a subclass?
    .......
    Do I have to have the "virtual" keyword in there? What benefit does it provide if it is not a pure virtual method?
    Yes, to be overridden a method needs to be declared virtual in the base class, but you don't have to repeat the virtual keyword at the overriding method in the derived class.

    If a virtual method isn't declared pure it has a default implementation that's used if it's not overridden.

  3. #3
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: virtual method question

    Quote Originally Posted by nuzzle View Post
    Yes, to be overridden a method needs to be declared virtual in the base class, but you don't have to repeat the virtual keyword at the overriding method in the derived class.
    Actually, a method can be overridden in the derived class even if it is not virtual in the base class. Just don't expect any runtime-virtual behavior, it's a static override. If you call the method via a base pointer, you'll get the base implementation.

    In real life C++ though, there is little to no use for this, and usually results to/from bugs.

    But to answer the OP, you don't have to write "virtual" again in the derived class, but I believe it is good practice to do so.

    Quote Originally Posted by nuzzle View Post
    If a virtual method isn't declared pure it has a default implementation that's used if it's not overridden.
    You may also have a pure virtual function with a default implementation. This happens when you want a pure virtual base class, but all virtual functions happen to have a default implementation. Then, you usually make the destructor (but it can be anything) a pure virtual, but implement it anyways.

    Also, http://en.wikipedia.org/wiki/C++0x#E...tion_overrides
    Last edited by monarch_dodra; May 3rd, 2010 at 09:29 AM.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  4. #4
    Join Date
    May 2009
    Posts
    2,413

    Re: virtual method question

    Quote Originally Posted by monarch_dodra View Post
    Actually, a method can be overridden in the derived class even if it is not virtual in the base class.
    I considered that before replying but after checking some reference litterature (including the C++ standard) I came to the conclusion that a non-virtual method cannot be overriden. You can try it but the method will not be overridden.

  5. #5
    Join Date
    Aug 2009
    Location
    Romania->Felnac
    Posts
    48

    Exclamation Re: virtual method question

    If you use any virtual function do not forget to use virtual destructor.

  6. #6
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: virtual method question

    Quote Originally Posted by nuzzle View Post
    I considered that before replying but after checking some reference litterature (including the C++ standard) I came to the conclusion that a non-virtual method cannot be overriden. You can try it but the method will not be overridden.
    What do you mean? Are we talking about the meaning of "override", or just that it is illegal for a derived class to declare a function with the same signature?

    I'd be interested in links to that literature.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  7. #7
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: virtual method question

    Quote Originally Posted by monarch_dodra
    What do you mean? Are we talking about the meaning of "override", or just that it is illegal for a derived class to declare a function with the same signature?
    It is a matter of what it means to override. What you are talking about is overloading, not overriding. (Actually, maybe not: it might just be name hiding.)

    Quote Originally Posted by monarch_dodra
    I'd be interested in links to that literature.
    For starters, read Stroustrup's definition of overriding.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  8. #8
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: virtual method question

    Using that definition, then it's correct to state that a non-virtual function in a base class cannot be overridden.

    However, it is perfectly possible to put a function of identical name and arguments into the derived class. Not a good idea, but possible.

  9. #9
    Join Date
    May 2009
    Posts
    2,413

    Re: virtual method question

    Quote Originally Posted by monarch_dodra View Post
    What do you mean? Are we talking about the meaning of "override", or just that it is illegal for a derived class to declare a function with the same signature?
    We are talking about the meaning of the term "override" in C++.

    You claimed this,

    "Actually, a method can be overridden in the derived class even if it is not virtual in the base class."

    That's wrong for the simple reason that non-virtual methods cannot be overridden. Overriding has a specific meaning that just doesn't apply to non-virtual methods.

    I'd be interested in links to that literature.
    Wasn't the C++ standard good enought?

  10. #10
    Join Date
    May 2009
    Posts
    2,413

    Re: virtual method question

    Quote Originally Posted by Lindley View Post
    Using that definition, then it's correct to state that a non-virtual function in a base class cannot be overridden.

    However, it is perfectly possible to put a function of identical name and arguments into the derived class. Not a good idea, but possible.
    Sure and that's an example of hiding, not overriding.
    Last edited by nuzzle; May 3rd, 2010 at 11:40 AM.

  11. #11
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: virtual method question

    I think everyone here understands that, it's simply a bit of terminology confusion.

  12. #12
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: virtual method question

    Quote Originally Posted by laserlight View Post
    It is a matter of what it means to override. What you are talking about is overloading, not overriding. (Actually, maybe not: it might just be name hiding.)

    For starters, read Stroustrup's definition of overriding.
    I did not realize that there was a special (and different) word for that. I now know there is a special technical term for that. I'll be using the correct terminology now.

    Although nuzzle is right, in this case it is hiding, not overriding or overloading either.

    Quote Originally Posted by nuzzle View Post
    Wasn't the C++ standard good enought?
    It always takes me something like 20 minutes to find something in the standard (when I find the standard at all, it isn't easy to find), so I decided to ask for a link instead. And an explanation, because the standard is usually too obscure to really explain anything. (it's good for references, but real crappy as far as learning goes)

    Sorry for asking.

    Quote Originally Posted by Lindley View Post
    I think everyone here understands that, it's simply a bit of terminology confusion.
    Thank you.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  13. #13
    Join Date
    May 2009
    Posts
    2,413

    Re: virtual method question

    Quote Originally Posted by monarch_dodra View Post
    Sorry for asking.
    I know every trick in the book so don't try that with me. You asked for links in arrogance convinced you were right and that I would dig my own grave if supplying links. It wasn't just an innocent question.

    Thank you.
    Why are you thanking those who are comforting you for being wrong rather than me who set you straight? Don't be such a bad loser.
    Last edited by nuzzle; May 3rd, 2010 at 08:47 PM.

  14. #14
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: virtual method question

    Quote Originally Posted by nuzzle View Post
    I know every trick in the book so don't try that with me. You asked for links in arrogance convinced you were right and would dig my own grave. It wasn't just an innocent question.
    If you had read his first post more carefully, you would have noticed that he described perfectly the behavior of name hiding, he simply applied the wrong term to describe it. It was a terminology problem from the start, and nothing more, so don't try to pretend you swooped in to correct some massively incorrect notion.

    Why are you thanking those who are comforting you for being wrong rather than me who set you straight? Don't be such a bad loser.
    I've tried to give you the benefit of the doubt, but you continue to demonstrate an attitude problem. Doesn't matter how smart you are, no one's going to want to hear what you have to say if you're an asshole about it.
    Last edited by Lindley; May 3rd, 2010 at 06:20 PM.

  15. #15
    Join Date
    May 2009
    Posts
    2,413

    Re: virtual method question

    Quote Originally Posted by Lindley View Post
    If you had read his first post more carefully,
    I did and I replied in measure. It was only after his second post I knew it was time to pull out the sledgehammer.

    Maybe I overdid it but there's something you should know about me. I have psychic powers. Whatever people write I know what they were thinking when they wrote it so they can never fool me.

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