473,395 Members | 1,678 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,395 software developers and data experts.

Covariants

I got a question about covariants and overloading. This is my code:

#include <iostream>
using namespace std;

class Parent1
{
};

class Parent2
{
public:
virtual Parent1 * f() { return new Parent1(); }
};

class Child1 : public Parent1
{
public:
class Child2 : public Parent2
{
public:
Child1 * f() { return new Child1(); }
};
};

int main()
{
int x;
cin >> x;
}

This code refuses to compile saying that overload of f() returns
non-covariant of Parent2::f()'s return.

This change compiles fine:

class Child1 : public Parent1
{
public:
};
class Child2 : public Parent2
{
public:
Child1 * f() { return new Child1(); }
};

As does this:
template<class T>
class Child1 : public Parent1
{
public:
class Child2 : public Parent2
{
public:
Child1 * f() { return new Child1(); }
};
};

int main()
{
Child1<int> c;
int x;
cin >> x;
}
I thought that any inner class had access to all names the outer class
does. The outer class knows what it is, why doesn't the inner class
know what the outer class is? Obviously Parent1 is not fully defined
when declaring Child2::f() but what is the rule I'm breaking in the
first example?

Apr 18 '06 #1
7 1409
Noah Roberts wrote:
I got a question about covariants and overloading. This is my code:

#include <iostream>
using namespace std;

class Parent1
{
};

class Parent2
{
public:
virtual Parent1 * f() { return new Parent1(); }
};

class Child1 : public Parent1
{
public:
class Child2 : public Parent2
{
public:
Child1 * f() { return new Child1(); }
};
};

int main()
{
int x;
cin >> x;
}

This code refuses to compile saying that overload of f() returns
non-covariant of Parent2::f()'s return.
Really? Comeau compiles it fine, so does VC++ v8.
This change compiles fine:

[..]

I thought that any inner class had access to all names the outer class
does. The outer class knows what it is, why doesn't the inner class
know what the outer class is? Obviously Parent1 is not fully defined
when declaring Child2::f() but what is the rule I'm breaking in the
first example?


The compiler you're using is obviously buggy.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 18 '06 #2

Victor Bazarov wrote:
Really? Comeau compiles it fine, so does VC++ v8. The compiler you're using is obviously buggy.


Hmmm....guess it must have been fixed later...this is v7.

Apr 18 '06 #3
Noah Roberts wrote:
Victor Bazarov wrote:
Really? Comeau compiles it fine, so does VC++ v8.

The compiler you're using is obviously buggy.


Hmmm....guess it must have been fixed later...this is v7.


Try (upgrade to) v7.1 or/and ask in 'microsoft.public.vc.language'.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 18 '06 #4

Victor Bazarov wrote:
Noah Roberts wrote:
Victor Bazarov wrote:
Really? Comeau compiles it fine, so does VC++ v8.

The compiler you're using is obviously buggy.


Hmmm....guess it must have been fixed later...this is v7.


Try (upgrade to) v7.1 or/and ask in 'microsoft.public.vc.language'.


No need Vic, that wasn't a question...hense the period.

Apr 18 '06 #5
Noah Roberts wrote:
Victor Bazarov wrote:
Noah Roberts wrote:
Victor Bazarov wrote:

Really? Comeau compiles it fine, so does VC++ v8.

The compiler you're using is obviously buggy.
Hmmm....guess it must have been fixed later...this is v7.


Try (upgrade to) v7.1 or/and ask in 'microsoft.public.vc.language'.


No need Vic, that wasn't a question...hense the period.


That wasn't an answer. It was advice.
Apr 18 '06 #6

Victor Bazarov wrote:
Noah Roberts wrote:
Victor Bazarov wrote:
Noah Roberts wrote:
Victor Bazarov wrote:

> Really? Comeau compiles it fine, so does VC++ v8.

> The compiler you're using is obviously buggy.
>

Hmmm....guess it must have been fixed later...this is v7.

Try (upgrade to) v7.1 or/and ask in 'microsoft.public.vc.language'.


No need Vic, that wasn't a question...hense the period.


That wasn't an answer. It was advice.


For what purpose. I don't need to be told to go post elsewhere. My
question was on-topic.

Apr 18 '06 #7
Noah Roberts wrote:
Victor Bazarov wrote:
Noah Roberts wrote:
Victor Bazarov wrote:
Noah Roberts wrote:
> Victor Bazarov wrote:
>
>> Really? Comeau compiles it fine, so does VC++ v8.
>
>> The compiler you're using is obviously buggy.
>>
>
> Hmmm....guess it must have been fixed later...this is v7.

Try (upgrade to) v7.1 or/and ask in 'microsoft.public.vc.language'.

No need Vic, that wasn't a question...hense the period.


That wasn't an answer. It was advice.


For what purpose. I don't need to be told to go post elsewhere. My
question was on-topic.


I am sorry, I didn't mean to suggest you were off-topic. I just pointed
you to that newsgroup because they know better about the differences between
v7 and v7.1 (if you don't want or can't upgrade). Lighten up, will you?
It is quite possible that an upgrade to 7.1 is free (again, I don't know,
but m.p.vc.l frequents might).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 18 '06 #8

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

Similar topics

12
by: Simon Elliott | last post by:
--- foo.h --- class foo { public: virtual void DoStuff(void); virtual ~foo(); }; --- bar.h --- #include "foo.h"
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()
30
by: Richard Tappenden | last post by:
Hi! I'm struggling a bit with Polymorphism in vb.net. There is something I know I can do in C++, but I cannot seem to do it in vb.net (or c# for that matter). Basically, I have an abstract...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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,...

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.