473,396 Members | 1,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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+(const A<C>&)'
mini.cpp:8: error: overriding 'A<TTypeA<TType>::operator+(const
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: SebastianSchucht
Dec 6 '07 #1
2 10287
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+(const A<C>&)'
mini.cpp:8: error: overriding 'A<TTypeA<TType>::operator+(const
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+(const A<C>&)'
mini.cpp:8: error: overriding 'A<TTypeA<TType>::operator+(const
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
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
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
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...
16
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
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...
8
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...
3
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...
9
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
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&...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.