7 Replies - 3980 Views - Last Post: 20 September 2008 - 12:05 AM Rate Topic: -----

#1 The Architect 2.0  Icon User is offline

  • D.I.C Regular

Reputation: 37
  • View blog
  • Posts: 351
  • Joined: 22-May 08

pure virtual function/inheritance question

Posted 18 September 2008 - 07:02 PM

just a quick question, concerning pure virtual functions, does the derived method need to have the same return type?

my c++ book says the return type can be different, but my VC++ compiler says otherwise.
Is This A Good Question/Topic? 0
  • +

#3 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 5145
  • View blog
  • Posts: 13,832
  • Joined: 18-April 07

Re: pure virtual function/inheritance question

Posted 18 September 2008 - 08:36 PM

Yes they can have different return types. This is called "Covariant Return Types" and is a feature not originally put into the language at first. If your compiler is complaining about it, it is probably because it is an older compiler. In this case you can either change to a newer compiler or simply change the return type to match.

But lookup "Covariant Return Types" for more information. :)
Was This Post Helpful? 1

#4 The Architect 2.0  Icon User is offline

  • D.I.C Regular

Reputation: 37
  • View blog
  • Posts: 351
  • Joined: 22-May 08

Re: pure virtual function/inheritance question

Posted 18 September 2008 - 09:55 PM

class MathComponent
{
public:
	virtual void getValue () = 0;	
};

class Numeral: public MathComponent
{
private:
	double constant;
public:
	Numeral (int newconstant):
		constant((double)newconstant)
		{}
	Numeral (double newconstant):
		constant(newconstant)
		{}
	double getValue ()
		{return constant;}
};


Quote

error C2555: 'Numeral::getValue': overriding virtual function return type differs and is not covariant from 'MathComponent::getValue'


so, did i just make a syntax mistake or is the VC++ 2008 Express Edition not compliant?
Was This Post Helpful? 0
  • +
  • -

#5 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 5145
  • View blog
  • Posts: 13,832
  • Joined: 18-April 07

Re: pure virtual function/inheritance question

Posted 18 September 2008 - 11:04 PM

Well this has always been a problem with past (previous to .NET) Microsoft compilers. But it should work fine in the .NET compilers IF the return type of the derived class is in the same class hierarchy. Obviously a double data type is not the same hierarchy of inheritance as void. So this error will need to be addressed by changing the base or derived class' return type to match the other.

But if you take a look at it, does it make sense to have a function called getValue and have it return void? Obviously with a name like getValue you want to return a value.

;)
Was This Post Helpful? 0
  • +
  • -

#6 The Architect 2.0  Icon User is offline

  • D.I.C Regular

Reputation: 37
  • View blog
  • Posts: 351
  • Joined: 22-May 08

Re: pure virtual function/inheritance question

Posted 19 September 2008 - 09:25 AM

well, the issue is that i want to create a 'Variable' class with its 'constant' variable being a char(and therefore its getValue method would return a char value).

i'm not quite sure i understand your explanation. are you saying that this code won't work at all(with any compliant compiler) or it SHOULD work with VC++, but for some reason doesn't? regardless, is there a solution?
Was This Post Helpful? 0
  • +
  • -

#7 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 5145
  • View blog
  • Posts: 13,832
  • Joined: 18-April 07

Re: pure virtual function/inheritance question

Posted 19 September 2008 - 11:29 PM

What I am saying is that covariant return types work in the compiler, but in your situation and purpose you have to alter the return types to match.

Now lets say you had two classes... one called shape and the other called circle. Circle inherits from shape. If the virtual function had a return type of shape and the other had circle, you would be good because they are the same hierarchy.

Example...

// Define two classes, shape and circle.
// circle inherits from shape.
class shape { };
class circle : public shape { };

class MathComponent
{
public:
	// Notice return type shape pointer
	virtual shape* getValue () = 0;	
};



class Numeral: public MathComponent
{
private:
	double constant;
public:
	Numeral (int newconstant):
		constant((double)newconstant)
		{}
	Numeral (double newconstant):
		constant(newconstant)
		{}

	// Notice that we return a pointer to circle, a type of shape.
	circle* getValue ()
		{return new circle();}
};



Notice in the code above that we have two new classes, shape and circle. In the MathComponent we have a virtual function that returns a type of shape. But in Numeral we override that and return a circle. Since circle is a type of shape, this will work fine. Thus we have overridden a function and provided a different return type.

But since you have double and void, the two are not related and thus you are going to have to change them to match or come up with your own classes and set them up like I have done above.

Hope things are clear now. :)
Was This Post Helpful? 0
  • +
  • -

#8 The Architect 2.0  Icon User is offline

  • D.I.C Regular

Reputation: 37
  • View blog
  • Posts: 351
  • Joined: 22-May 08

Re: pure virtual function/inheritance question

Posted 19 September 2008 - 11:59 PM

thanks a lot, i get it.

but do built-in types have hierarchies?
Was This Post Helpful? 0
  • +
  • -

#9 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 5145
  • View blog
  • Posts: 13,832
  • Joined: 18-April 07

Re: pure virtual function/inheritance question

Posted 20 September 2008 - 12:05 AM

No not in C++, but in .NET they do because they are actually objects there. Datatypes in regular C++ are value types that are not inherited from any base class like "object" is in VC++/.NET. Thus your problem. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1