Covariants

Discussion in 'C++' started by Noah Roberts, Apr 18, 2006.

  1. Noah Roberts

    Noah Roberts Guest

    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?
     
    Noah Roberts, Apr 18, 2006
    #1
    1. Advertising

  2. 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
     
    Victor Bazarov, Apr 18, 2006
    #2
    1. Advertising

  3. Noah Roberts

    Noah Roberts Guest

    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.
     
    Noah Roberts, Apr 18, 2006
    #3
  4. 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
     
    Victor Bazarov, Apr 18, 2006
    #4
  5. Noah Roberts

    Noah Roberts Guest

    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.
     
    Noah Roberts, Apr 18, 2006
    #5
  6. 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.
     
    Victor Bazarov, Apr 18, 2006
    #6
  7. Noah Roberts

    Noah Roberts Guest

    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.
     
    Noah Roberts, Apr 18, 2006
    #7
  8. 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
     
    Victor Bazarov, Apr 18, 2006
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page