Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 657159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:47:34+00:00 2026-05-13T22:47:34+00:00

I have a quite complex class hierarchy in which the classes are cross-like depending

  • 0

I have a quite complex class hierarchy in which the classes are cross-like depending on each other: There are two abstract classes A and C containing a method that returns an instance of C and A, respectively. In their inherited classes I want to use a co-variant type, which is in this case a problem since I don’t know a way to forward-declare the inheritance relation ship.

I obtain a “test.cpp:22: error: invalid covariant return type for ‘virtual D* B::outC()’”-error since the compiler does not know that D is a subclass of C.

class C;

class A {
public:
        virtual C* outC() = 0;
};

class C {
public:
        virtual A* outA() = 0;
};


class D;

class B : public A {
public:
        D* outC();
};

class D : public C {
public:
        B* outA();
};

D* B::outC() {
        return new D();
}

B* D::outA() {
        return new B();
}

If I change the return type of B::outC() to C* the example compiles. Is there any way to keep B* and D* as return types in the inherited classes (it would be intuitive to me that there is a way)?

  • 1 1 Answer
  • 3 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T22:47:35+00:00Added an answer on May 13, 2026 at 10:47 pm

    I know of no way of having directly coupled covariant members in C++. You’ll have either to add a layer, or implement covariant return yourself.

    For the first option

    class C;
    
    class A {
    public:
            virtual C* outC() = 0;
    };
    
    class C {
    public:
            virtual A* outA() = 0;
    };
    
    
    class BI : public A {
    public:
    };
    
    class D : public C {
    public:
            BI* outA();
    };
    
    class B: public BI {
    public:
            D* outC();
    };
    
    D* B::outC() {
            return new D();
    }
    
    BI* D::outA() {
            return new B();
    }
    

    and for the second

    class C;
    
    class A {
    public:
            C* outC() { return do_outC(); }
            virtual C* do_outC() = 0;
    };
    
    class C {
    public:
            virtual A* outA() = 0;
    };
    
    
    class D;
    
    class B : public A {
    public:
            D* outC();
            virtual C* do_outC();
    };
    
    class D : public C {
    public:
            B* outA();
    };
    
    D* B::outC() {
            return static_cast<D*>(do_outC());
    }
    
    C* B::do_outC() {
            return new D();
    }
    
    B* D::outA() {
            return new B();
    }
    

    Note that this second option is what is done implicitly by the compiler (with some static checks that the static_cast is valid).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We currently have quite a few classes in a project, and each of those
Summary Let's say I have two C# 4.0 classes, one inheriting from the other:
I have a class which inherits QAbstractTableModel, and holds some complex structs in a
I have quite a complex entity structure where several classes inherit from a base
I have quite a complex piece of code (JQuery and HTML5 Web SQL) that
I have a quite complex multi-join TSQL SELECT query that runs for about 8
I hope I will find a help here. I have a quite complex MVC
I have a script that works great. It is quite complex and does numerous
I have quite a large VB6 project, with many forms, classes, modules and user
Suppose I have a class like this: public class ThingManager { List<SomeClass> ItemList; public

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.