473,698 Members | 2,551 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

invalid covariant return type

Hi,

I have a templateclass with virtual member-functions, so i can't create
instances ... but for returning the result i would like use one. So i
replaced the A<TTypefrom the baseclass with the Type from the spezialized
class. After this, the error invalid covariant return type occoured.

the following minimal sample shows the problem:

#include <iostream>
class C{};

template<class TType>
class A
{
public:
virtual A<TTypeoperator +(const A<TType>& i_sum) = 0;
};

class B:public A<C>
{
public:
B operator+(const A<C>& i_sum) {return *this;};
};

main()
{
B a,b,c;
a+b;
}

After compiling with g++ version 4.1.2 (Ubuntu 4.1.2-0ubuntu4) i have the
following erros on screen:

mini.cpp:14: error: invalid covariant return type for 'virtual B
B::operator+(co nst A<C>&)'
mini.cpp:8: error: overriding 'A<TTypeA<TType >::operator+(co nst
A<TType>&) [with TType = C]'

- Why is this an error?
- On wich way i can fix it?

Thank you for your help!

With best regards

Sebastian Schucht

--
B.Sc. Sebastian Schucht
Teichhausstraß e 38
64287 Darmstadt

UIN: 260 121 043
skype: SebastianSchuch t
Dec 6 '07 #1
2 10318
Sebastian Schucht wrote:
I have a templateclass with virtual member-functions, so i can't
create instances ... but for returning the result i would like use
one. So i replaced the A<TTypefrom the baseclass with the Type from
the spezialized class. After this, the error invalid covariant return
type occoured.
"Covariant" is either a pointer or a reference. With objects, they
have to be _exactly the same_ to be covariant.
the following minimal sample shows the problem:

#include <iostream>
class C{};

template<class TType>
class A
{
public:
virtual A<TTypeoperator +(const A<TType>& i_sum) = 0;
};

class B:public A<C>
{
public:
B operator+(const A<C>& i_sum) {return *this;};
};

main()
{
B a,b,c;
a+b;
What's the 'c' object for?
}

After compiling with g++ version 4.1.2 (Ubuntu 4.1.2-0ubuntu4) i have
the following erros on screen:

mini.cpp:14: error: invalid covariant return type for 'virtual B
B::operator+(co nst A<C>&)'
mini.cpp:8: error: overriding 'A<TTypeA<TType >::operator+(co nst
A<TType>&) [with TType = C]'

- Why is this an error?
A<Cis not covariant with B, they are not the same type. A<C>& would
be covariant with B&. A<C>* would be covariant with B*.
- On wich way i can fix it?
You can't. Don't make operators virtual, it's unnatural since they
return objects (where slicing will most likely occur).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 6 '07 #2
Sebastian Schucht wrote:
Hi,

I have a templateclass with virtual member-functions, so i can't create
instances ... but for returning the result i would like use one. So i
replaced the A<TTypefrom the baseclass with the Type from the
spezialized class. After this, the error invalid covariant return type
occoured.

the following minimal sample shows the problem:

#include <iostream>
class C{};

template<class TType>
class A
{
public:
virtual A<TTypeoperator +(const A<TType>& i_sum) = 0;
};

class B:public A<C>
{
public:
B operator+(const A<C>& i_sum) {return *this;};
};

main()
{
B a,b,c;
a+b;
}

After compiling with g++ version 4.1.2 (Ubuntu 4.1.2-0ubuntu4) i have the
following erros on screen:

mini.cpp:14: error: invalid covariant return type for 'virtual B
B::operator+(co nst A<C>&)'
mini.cpp:8: error: overriding 'A<TTypeA<TType >::operator+(co nst
A<TType>&) [with TType = C]'

- Why is this an error?
Because of clause [10.3/10]:
The return type of an overriding function shall be either identical to the
return type of the overridden function or covariant with the classes of
the functions. If a function D::f overrides a function B::f, the return
types of the functions are covariant if they satisfy the following
criteria:
? both are pointers to classes or references to classes
...

As you can see, it only works for pointers or references.

- On wich way i can fix it?
Return a reference.

Now, that will very likely not work for operator+ (if it does something
remotely similar to adding things). However, you could do it for operator+=
because that has a reasonable claim to return *this.

Better advice might be possible if you provide some background as to what
the underlying problem is that led you to consider a virtual operator+ in
the first place.
Best

Kai-Uwe Bux
Dec 7 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
2148
by: Alex Vinokur | last post by:
Hello, Here is some program with virtual constructors. Is there any difference between * clone1() vs. clone2() * create1() vs. create2() ? It seems that it should be.
14
1998
by: Stefan Slapeta | last post by:
Hi, this code does not compile in C#: class base_class {} class derived_class : base_class {} class A { public virtual base_class f()
13
4264
by: Stephen Walch | last post by:
Error C2392 is hitting me hard! I have a managed C++ library that implements a bunch of fixed interfaces. For example, one interface is: public abstract interface IDbCommand { public abstract new System.Data.IDbConnection Connection }
16
3198
by: Bob Hairgrove | last post by:
Consider the classic clone() function: class A { public: virtual ~A() {} virtual A* clone() const = 0; }; class B : public A { public:
6
2549
by: miked | last post by:
Why are there still no covariant return types? All searches reveal no workarounds accept for using an interface which is a real pain. I wind up missing this capability almost every time I inherit a class. The best workaround and one that I use is the new keyword and cast the base class reference, but in my opinion this is a bad practice leading to error prone code. Can someone from MS chime in on this and provide some feedback?
8
2175
by: Alex Vinokur | last post by:
Here is a code from http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.8 -------------------------------------- class Shape { public: virtual ~Shape() { } // A virtual destructor virtual void draw() = 0; // A pure virtual function virtual void move() = 0; ...
3
4549
by: kikazaru | last post by:
Is it possible to return covariant types for virtual methods inherited from a base class using virtual inheritance? I've constructed an example below, which has the following structure: Shape = base class Triangle, Square = classes derived from Shape Prism = class derived from Shape TriangularPrism, SquarePrism = classes derived from Triangle and Prism, or Square and Prism respectively
9
1875
by: Rahul | last post by:
Hi Everyone, I was trying to implement covariant return types and i get a compilation error, class BB : public AA { }; class A
3
2122
by: abir | last post by:
Hi, is there any way forward declare a covariant return type (and is it a language feature or extension only)? I have class, class CompModel; class CompWorker{ public: virtual CompModel& model(); /// forward declaration works as usual.
0
8609
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9030
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8899
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6528
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5861
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4371
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2335
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.