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 6731447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:31:47+00:00 2026-05-26T10:31:47+00:00

In a design of a class hierarchy, I’m using an abstract base class that

  • 0

In a design of a class hierarchy, I’m using an abstract base class that declares various methods that the derived classes would implement. In a sense, the base class is as close to an interface as you can get in C++. However, there is an specific issue. Consider the code below which declares our interface class:

class Interface {
public:
    virtual Interface method() = 0;
};

class Implementation : public Interface {
public:
    virtual Implementation method() { /* ... */ }
};

Of course, this wouldn’t compile, because you cannot return an abstract class in C++. To get around this problem I’m using the following solution:

template <class T>
class Interface {
public:
    virtual T method() = 0;
};

class Implementation : public Interface<Implementation> {
public:
    virtual Implementation method() { /* ... */ }
};

This solution works and is all fine and dandy, however, to me, it doesn’t look very elegant, because of the redundant bit of text which would be the parameter for interface. I’d be happy if you guys could point our any other technical issues with this design, but that is my only concern at this point.

Is there any way to get rid of that redundant template parameter? Possibly using macros?

Note: The method in question has to return an instance. I’m aware that if method() returned a pointer or a reference, there would be no issue.

  • 1 1 Answer
  • 0 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-26T10:31:47+00:00Added an answer on May 26, 2026 at 10:31 am

    Interface::method() cannot return an Interface instance without using a pointer or reference. Returning a non-pointer, non-reference Interface instance requires instantiating an instance of Interface itself, which is illegal because Interface is abstract. If you want the base class to return an object instance, you have to use one of the following:

    A pointer:

    class Interface
    {
    public:
      virtual Interface* method() = 0;
    };
    
    class Implementation : public Interface
    {
    public:
      virtual Interface* method() { /* ... */ }
    };
    

    A reference:

    class Interface
    {
    public:
      virtual Interface& method() = 0;
    };
    
    class Implementation : public Interface
    {
    public:
      virtual Interface& method() { /* ... */ }
    };
    

    A template parameter:

    template<type T>
    class Interface
    {
    public:
      virtual T method() = 0;
    };
    
    class Implementation : public Interface<Implementation>
    {
    public:
      virtual Implementation method() { /* ... */ }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to design a class hierarchy with a bunch of similar classes that
I have a small hierarchy of classes that all implement a common interface. Each
I'm taking a compiler-design class where we have to implement our own compiler (using
In my program I have the following class hierarchy: class Base // Base is
I'm working on an assignment has a class hierarchy like blow: abstract EventClass someEventClass1
I heave roughly speaking the following hierarchy class Base { public: virtual int value()
Let's consider the following simplified Resource hierarchy: public abstract class Resource { static public
Imagine I have such class hierarchy: Base A : Base B : Base C
I have the following class hierarchy [BsonKnownTypes(typeof(MoveCommand))] public abstract class Command : ICommand {
I have a class hierarchy with the following three classes: template<int pdim > class

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.