473,545 Members | 1,351 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

covariant return types

Hi Everyone,

I was trying to implement covariant return types and i get a
compilation error,

class BB : public AA
{
};

class A
{
public: virtual A* sample()
{
A obj;
return(&obj);
}
};

class B : public A
{
public: virtual B* sample()
{
B obj;
return(&obj);
}
};

'B::sample' : overriding virtual function differs from 'A::sample'
only by return type or calling convention

Am i missing something?

Thanks in advance!!!
Dec 18 '07 #1
9 1862
On Dec 18, 11:11 pm, Rahul <sam_...@yahoo. co.inwrote:
Hi Everyone,

I was trying to implement covariant return types and i get a
compilation error,

class BB : public AA
{

};
What are these classes? BB/AA?
class A
{
public: virtual A* sample()
{
A obj;
return(&obj);
}

};
Returning pointer to a local object!!
class B : public A
{
public: virtual B* sample()
{
B obj;
return(&obj);
}

};
Returning pointer to a local object!!
>
'B::sample' : overriding virtual function differs from 'A::sample'
only by return type or calling convention

Am i missing something?
What compiler is it? I think it should compile just fine if you
correct the local object pointer return thing, and remove the class BB
and AA (they are useless in the rest of your code).
Dec 18 '07 #2
On Dec 18, 11:16 pm, Abhishek Padmanabh <abhishek.padma n...@gmail.com>
wrote:
On Dec 18, 11:11 pm, Rahul <sam_...@yahoo. co.inwrote:
Hi Everyone,
I was trying to implement covariant return types and i get a
compilation error,
class BB : public AA
{
};

What are these classes? BB/AA?
class A
{
public: virtual A* sample()
{
A obj;
return(&obj);
}
};

Returning pointer to a local object!!
class B : public A
{
public: virtual B* sample()
{
B obj;
return(&obj);
}
};

Returning pointer to a local object!!
'B::sample' : overriding virtual function differs from 'A::sample'
only by return type or calling convention
Am i missing something?

What compiler is it? I think it should compile just fine if you
correct the local object pointer return thing, and remove the class BB
and AA (they are useless in the rest of your code).
Ignore the dummy class BB and the logic of the function returning
address of local object.
Yes i do get a warning from the compiler, however the error is with
the return type of functions.
I tried this on gcc... not sure about the version though...
Dec 18 '07 #3
On Dec 18, 11:32 pm, Rahul <sam_...@yahoo. co.inwrote:
I tried this on gcc... not sure about the version though...- Hide quoted text -
Try running the command g++ -v to see the version.
Dec 18 '07 #4
Rahul wrote:
Hi Everyone,

I was trying to implement covariant return types and i get a
compilation error,

class BB : public AA
{
};

class A
{
public: virtual A* sample()
{
A obj;
return(&obj);
Change to return new A;
}
};

class B : public A
{
public: virtual B* sample()
{
B obj;
return(&obj);
Change to return new B;
}
};

'B::sample' : overriding virtual function differs from 'A::sample'
only by return type or calling convention

Am i missing something?
There isn't anything wrong with the return types, just the returns of
local objects.

--
Ian Collins.
Dec 18 '07 #5
Rahul schrieb:
Hi Everyone,

I was trying to implement covariant return types and i get a
compilation error,

class BB : public AA
{
};

class A
{
public: virtual A* sample()
{
A obj;
return(&obj);
}
};

class B : public A
{
public: virtual B* sample()
{
B obj;
return(&obj);
}
};

'B::sample' : overriding virtual function differs from 'A::sample'
only by return type or calling convention

Am i missing something?

Thanks in advance!!!
Hi!

The return type is part of the internal method name. When overloading a
method you need exactly the same internal name and - additionally - it
is not possible to make two identical methods which differ only in the
return type.

Maybe you could templates for this.

Andreas
Dec 18 '07 #6
Rahul wrote:
I was trying to implement covariant return types and i get a
compilation error,

class BB : public AA
{
};

class A
{
public: virtual A* sample()
{
A obj;
return(&obj);
}
};

class B : public A
{
public: virtual B* sample()
{
B obj;
return(&obj);
}
};

'B::sample' : overriding virtual function differs from 'A::sample'
only by return type or calling convention

Am i missing something?
Unless you actually forgot to derive 'B' from 'A' in your real
code, the types of functions 'A::sample' and 'B::sample' are OK
(they are covariant). You may be missing the fact that you're
using an outdated compiler that doesn't understand covariant
return types.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 18 '07 #7
On Dec 18, 3:11 pm, Rahul <sam_...@yahoo. co.inwrote:
Hi Everyone,

I was trying to implement covariant return types and i get a
compilation error,

class BB : public AA
{

};

class A
{
public: virtual A* sample()
{
A obj;
return(&obj);
}

};

class B : public A
{
public: virtual B* sample()
{
B obj;
return(&obj);
}

};

'B::sample' : overriding virtual function differs from 'A::sample'
only by return type or calling convention

Am i missing something?

Thanks in advance!!!
You are using VC++ 6, which doesn't support covariant return types.
Please update your compiler, it's too old...
Regards

--
Cholo Lennon
Bs.As.
ARG
Dec 18 '07 #8
Andreas Hammerschmidt wrote:
Rahul schrieb:
>Hi Everyone,

I was trying to implement covariant return types and i get a
compilation error,

class BB : public AA
{
};

class A
{
public: virtual A* sample()
{
A obj;
return(&obj);
}
};

class B : public A
{
public: virtual B* sample()
{
B obj;
return(&obj);
}
};

'B::sample' : overriding virtual function differs from 'A::sample'
only by return type or calling convention

Am i missing something?

Thanks in advance!!!

Hi!

The return type is part of the internal method name. When overloading a
method you need exactly the same internal name and - additionally - it
is not possible to make two identical methods which differ only in the
return type.
Except in this special case, which is a covariant return. That is, the
override returns a pointer (or reference) to a child of what the base
class function returns.

It's explicitly allowed by the standard.
Dec 19 '07 #9
On Dec 18, 7:11 pm, Rahul <sam_...@yahoo. co.inwrote:
Hi Everyone,

I was trying to implement covariant return types and i get a
compilation error,
The code is supposed to compile. Try compilation under

http://www.comeaucomputing.com/tryitout/

I find it to be very close to the standard and use it as
a benchmark (you could also go to
http://www.dinkumware.com/exam/default.aspx for the same
reason).

If I was you I would at least change the local
members to static members to prevent people from missing
the point of your question. BB/AA also does not seem to
contribute to your question.

Regards,

Werner
Dec 19 '07 #10

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.
3
1539
by: Sankar Nemani | last post by:
Hi, Does anyone know what the reason behind not allowing to use "covariant return types when overriding methods in derived classes" in C# is? Also while other OO languages such as Java, don't allow todo the same, why does C++ allow it and then the C# and VB.NET don't allow? I just want to understand the logical reasoning behind this...
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 }
2
1469
by: Mike | last post by:
I keep running into the scenario below over and over again. Currently I get around not having covariant return types by using an interface and explicit property definitions which works to some extent, but causes other problems that occur with explicit definitions and also leads to much more code that is also more difficult to maintain. The...
16
3178
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
2542
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
4538
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...
0
7401
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
7656
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7807
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
7419
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...
0
5971
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5326
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
3450
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
3442
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1879
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

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.