-
May 3rd, 2010, 08:43 AM
#1
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!
-
May 3rd, 2010, 09:18 AM
#2
Re: virtual method question
Originally Posted by lab1
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.
-
May 3rd, 2010, 09:26 AM
#3
Re: virtual method question
Originally Posted by nuzzle
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.
Originally Posted by nuzzle
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.
-
May 3rd, 2010, 10:18 AM
#4
Re: virtual method question
Originally Posted by monarch_dodra
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.
-
May 3rd, 2010, 10:41 AM
#5
Re: virtual method question
If you use any virtual function do not forget to use virtual destructor.
-
May 3rd, 2010, 11:01 AM
#6
Re: virtual method question
Originally Posted by nuzzle
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.
-
May 3rd, 2010, 11:25 AM
#7
Re: virtual method question
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.)
Originally Posted by monarch_dodra
I'd be interested in links to that literature.
For starters, read Stroustrup's definition of overriding.
-
May 3rd, 2010, 11:29 AM
#8
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.
-
May 3rd, 2010, 11:34 AM
#9
Re: virtual method question
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?
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?
-
May 3rd, 2010, 11:36 AM
#10
Re: virtual method question
Originally Posted by Lindley
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.
-
May 3rd, 2010, 11:37 AM
#11
Re: virtual method question
I think everyone here understands that, it's simply a bit of terminology confusion.
-
May 3rd, 2010, 12:58 PM
#12
Re: virtual method question
Originally Posted by laserlight
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.
Originally Posted by nuzzle
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.
Originally Posted by Lindley
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.
-
May 3rd, 2010, 05:52 PM
#13
Re: virtual method question
Originally Posted by monarch_dodra
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.
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.
-
May 3rd, 2010, 06:17 PM
#14
Re: virtual method question
Originally Posted by nuzzle
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.
-
May 3rd, 2010, 06:46 PM
#15
Re: virtual method question
Originally Posted by Lindley
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|