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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:58:11+00:00 2026-06-07T00:58:11+00:00

Suppose I have a pure virtual method in the base interface that returns to

  • 0

Suppose I have a pure virtual method in the base interface that returns to me a list of something:

class base 
{ 
public:
     virtual std::list<something> get() = 0; 
}; 

Suppose I have two classes that inherit the base class:

class A : public base 
{ 
public:
     std::list<something> get();
}; 

class B : public base 
{ 
public:
     std::list<something> get(); 
};

I want that only the A class can return a list<something>, but I need also to have the possibility to get the list using a base pointer, like for example:

base* base_ptr = new A();
base_ptr->get();

What I have to do?

Have I to return a pointer to this list? A reference?

Have I to return a null pointer from the method of class B? Or have I to throw an exception when I try to get the list using a B object? Or have I to change the base class method get, making it not pure and do this work in the base class?

Have I to do something else?

  • 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-07T00:58:13+00:00Added an answer on June 7, 2026 at 12:58 am

    You have nothing else to do. The code you provide does exactly that.

    When you get a pointer to the base class, since the method was declared in the base class, and is virtual, the actual implementation will be looked up in the class virtual function table and called appropriately.

    So

    base* base_ptr = new A();
    base_ptr->get();
    

    Will call A::get(). You should not return null from the implementation (well you can’t, since null is not convertible to std::list< something > anyway). You have to provide an implementation in A/B since the base class method is declared pure virtual.

    EDIT:

    you cannot have only A return an std::list< something > and not B since B also inherits the base class, and the base class has a pure virtual method that must be overriden in the derived class. Inheriting from a base class is a "is-a" relationship. The only other way around I could see would be to inherit privately from the class, but that would prevent derived to base conversion.

    If you really don’t want B to have the get method, don’t inherit from base.
    Some alternatives are:

    Throwing an exception in B::get():
    You could throw an exception in B::get() but make sure you explain your rationale well as it is counter-intuitive. IMHO this is pretty bad design, and you risk confusing people using your base class. It is a leaky abstraction and is best avoided.

    Separate interface:
    You could break base into separate interface for that matter:

    class IGetSomething
    {
    public:
        virtual ~IGetSomething() {}
        virtual std::list<something> Get() = 0;
    };
    
    class base
    {
    public:
        // ...
    };
    
    class A : public base, public IGetSomething
    {
    public:
        virtual std::list<something> Get()
        {
            // Implementation
            return std::list<something>();
        }
    };
    
    class B : public base
    {
    
    };
    

    The multiple inheritance in that case is OK because IGetSomething is a pure interface (it does not have member variables or non-pure methods).

    EDIT2:

    Based on the comments it seems you want to be able to have a common interface between the two classes, yet be able to perform some operation that one implementation do, but the other doesn’t provide. It is quite a convoluted scenario but we can take inspiration from COM (don’t shoot me yet):

    class base
    {
    public:
        virtual ~base() {}
        // ... common interface
    
        // TODO: give me a better name
        virtual IGetSomething *GetSomething() = 0;
    };
    
    class A : public Base
    {
    public:
        virtual IGetSomething *GetSomething()
        {
            return NULL;
        }
    };
    
    class B : public Base, public IGetSomething
    {
    public:
        virtual IGetSomething *GetSomething()
        {
            // Derived-to-base conversion OK
            return this;
        }
    };
    

    Now what you can do is this:

    base* base_ptr = new A();
    IGetSomething *getSmthing = base_ptr->GetSomething();
    if (getSmthing != NULL)
    {
        std::list<something> listOfSmthing = getSmthing->Get();
    }
    

    It is convoluted, but there are several advantages of this method:

    • You return public interfaces, not concrete implementation classes.

    • You use inheritance for what it’s designed for.

    • It is hard to use mistakenly: base does not provide std::list get() because it is not a common operation between the concrete implementation.

    • You are explicit about the semantics of GetSomething(): it allows you to return an interface that can be use to retrieve a list of something.

      What about just returning an empty std::list ?

    That would be possible but bad design, it’s like having a vending machine that can give Coke and Pepsi, except it never serves Pepsi; it’s misleading and best avoided.

    What about just returning a boost::optional< std::list< something > > ? (as suggested by Andrew)

    I think that’s a better solution, better than returning and interface that sometimes could be NULL and sometimes not, because then you explicitly know that it’s optional, and there would be no mistake about it.

    The downside is that it puts boost inside your interface, which I prefer to avoid (it’s up to me to use boost, but clients of the interface shouldn’t have to be forced to use boost).

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

Sidebar

Related Questions

Suppose I have a static method of my class that returns an object of
Suppose I have this class hierarchy: class A { public: virtual void foo(Base *b)
Suppose you have the following object hierarchy: class Vehicle { public: virtual ~Vehicle() {}
I have 2 classes: class Base { public: virtual int Foo(int n); virtual void
Suppose I have a class Baz that inherits from classes Foo and Bar ,
Suppose I have a simple model, such as Record: @Model public class Record {
Pure virtual functions are those member functions that are virtual and have the pure-specifier
Suppose that in a Haskell program I have some data whose type is something
Suppose I have a large list of words. For an example: >>> with open('/usr/share/dict/words')
Suppose I have a process that is updating a record and encounters a record

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.