By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,946 Members | 745 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,946 IT Pros & Developers. It's quick & easy.

Covariants

P: n/a
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
Share this Question
Share on Google+
7 Replies


P: n/a
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

P: n/a

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

P: n/a
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

P: n/a

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

P: n/a
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

P: n/a

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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.