473,465 Members | 1,867 Online
Bytes | Software Development & Data Engineering Community
Create 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 1856
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.padman...@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
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
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...
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...
2
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...
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...
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
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...
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
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...
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,...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.