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

  • Home
  • SEARCH
  • 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 8187909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T02:43:27+00:00 2026-06-07T02:43:27+00:00

I like to have well defined interface in a few classes, for this I

  • 0

I like to have well defined interface in a few classes, for this I created pure virtual functions in an abstract class that each class needs to implement.
But I’m facing the problem that I can instantiate the classes so I have to create another class that inherit the interface class, and all the other classes needs to inherit from this base class. Here is the code for example:

The interface

class Interface
{
     virtual std::string getName() = 0;
}

class Base : public Interface
{
   virtual std::string getName(return std::string("Base") ;)
}

class A : public Base
{
  std::string getName(return std::string("A") ;)

}

class B : public Base
{
  std::string getName(return std::string("B") ;)

}

All this so i could in code to have the same type for A & B.
Can I just use the Interface class without the Base class?
Something like this:

class A : public Interface
{
  std::string getName(return std::string("A") ;)

}
  • 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-06-07T02:43:31+00:00Added an answer on June 7, 2026 at 2:43 am

    You cannot instantiate the interface, but what you can do is have a pointer to the interface, pointing to an instance of an implementation:

    class Interface { /* pure virtual methods */ };
    class A : virtual public Interface { /* implement all pure virtual methods */ };
    class B : virtual public Interface { /* implement all pure virtual methods */ };
    
    Interface * i0 = new A();
    Interface * i1 = new B();
    

    This is a standard way of using polymorphic classes (although you might want to use smart pointers instead of raw pointers).

    Edit: concerning the use of virtual inheritance, see here and here for more information.

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

Sidebar

Related Questions

Requirement: I'd like all implementations of an interface to have a well-defined name. Initially,
well i have a configuration like this in the components part of my config
I have a text like: I've got a date with this fellow tomorrow. Well
well the calculation in my app goes like this.. have a edittext box where
Well, I'll say it like this. I have a table with 2 columns. The
I'd like to define an interface and abstract base class for a set of
I have an interface and a class defined as below public interface IShape {
So I have a FileReader class, it looks like this: #define DISALLOW_COPY(type) \ type(const
I have a well div, and I would like to attach a small text/image
I have a well-developed application for a phone, but I would like to add

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.