473,546 Members | 2,214 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 10310
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
2142
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
1989
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
4239
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
3181
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
2543
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...
8
2169
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
4540
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...
9
1864
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
2115
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
7507
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7435
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...
0
7947
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...
1
5361
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...
0
5080
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...
0
3492
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...
0
3472
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1922
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1046
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.