[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Abstract base class question C++

Posted on 2011-10-15
5
Medium Priority
?
438 Views
Last Modified: 2012-08-14
I I have an abstract base class in c++ as follows:
	class IMyBaseClass
	{
	public :
		virtual ~IMyBaseClass() {}
		virtual void AddSomeInformation( int param1, int param2) = 0;
		virtual void AddSomeMoreInformation( int param1) = 0;
	};

Open in new window


When I go to implement a class with this interface as follows:

       class MyBaseClass : public IMyBaseClass
	{
	public :
		virtual ~IMyBaseClass() {}
		void AddSomeInformation( int param1, int param2);
		void AddSomeMoreInformation( int param1);
	};

Open in new window


Do I have to include the word virtual before the function name in this class definition??
e.g.
virtual void AddSomeInformation( int param1, int param2);

Open in new window


instead of just:
void AddSomeInformation( int param1, int param2);

Open in new window


Can you explain your answer please?
0
Comment
Question by:Wanting2LearnMan
  • 2
  • 2
5 Comments
 
LVL 45

Accepted Solution

by:
AndyAinscow earned 400 total points
ID: 36973310
Add the word virtual - that instructs the compiler there is a base version of that function being overwritten.  Without it then the wrong function could be called at times depending on what object is being processed.
0
 
LVL 40

Assisted Solution

by:evilrix
evilrix earned 400 total points
ID: 36973429
>> Do I have to include the word virtual before the function name in this class definition??

In ANSI Standard C++, no. As long as the base class members are virtual all other derived classes will also inherit their "virtualness".
0
 
LVL 40

Assisted Solution

by:evilrix
evilrix earned 400 total points
ID: 36973433
>> In ANSI Standard C++, no.
Although doing so will not cause any harm... ie, it's optional.

>> Can you explain your answer please?

Cos, that's what the C++ standard says :)

"If a virtual member function vf is declared in a class Base and in a class Derived, derived directly or
indirectly from Base, a member function vf with the same name and same parameter list as Base::vf is
declared, then Derived::vf is also virtual (whether or not it is so declared) and it overrides
Base::vf."
0
 
LVL 45

Assisted Solution

by:AndyAinscow
AndyAinscow earned 400 total points
ID: 36978101
A couple of points.

evilrix knows the standards inside out - so the 'correct' answer is no, you don't require the keyword virtual.

HOWEVER - that requires the compiler to adhere to the standards.  You often see information in the help files such as microsoft specific - so that compiler is not adhering to the standards in all cases.  I'd still strongly recommend you always use the virtual keyword for the function in the derived classes when it is virtual in the base class.  That way it will be treated as a virtual function - no danger of some wierd and difficult to trace behaviour.



The other part of my comment:  I am pretty certain I saw a question here some time ago where the wrong version of a function was being called.  Maybe I remember incorrectly but I think it was that in one class it had been declared virtual in and in another the virtual keyword had been left out. Once the virtual keyword was added then the problem disappeared.
0
 
LVL 35

Assisted Solution

by:sarabande
sarabande earned 200 total points
ID: 36978891
Andy, i never have seen a compiler which was wrong in that case. it would be a fatal error which could not remain undetected for long as i know many libraries and software packages which have the virtual only in the very top baseclass.

when the compiler calls a wrong function, the error should be that name or argument types don't match exactly.  i mean to remember such a case recently.

Sara
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question