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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:53:54+00:00 2026-05-25T01:53:54+00:00

Lets say I have an abstract base class with a pure virtual that returns

  • 0

Lets say I have an abstract base class with a pure virtual that returns an expensive object. As it’s an expensive object, I should return a reference to it.

But life’s not that simple, let’s say I have two classes derived from it: one has the function called often, so it is more efficient to store a copy in the instance and return a reference. The other is called rarely, so it is better to create the object on demand to save RAM.

I thought I could just use covariance because the Liskov substitution principle would be happy, but of course Obj is not a subtype of Obj&, so compile errors result.

class abc
{
public:
    virtual BigObj& obj() = 0;
};

class derived : public abc
{
public:
    ...
    virtual BigObj obj() { return obj_; }

private:
    BigObj obj_;
};

Results in:

conflicting return type specified for ‘virtual BigObj derived::obj()’

Is there a more elegant solution to this than simply picking the least worst?

  • 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-25T01:53:56+00:00Added an answer on May 25, 2026 at 1:53 am

    One solution is to create a smart pointer class to manage BigObj*s:

    class BigObjPtr {
    public:
        BigObjPtr(bool del, BigObj* ptr) : del(del), ptr(ptr) { }
    
        BigObj* operator->() {
            return ptr;
        }
    
        virtual ~BigObjPtr() {
            if (del) delete ptr;
        }
    
    private:
        BigObj* ptr;
        bool del;
    };
    

    Then change your classes to return one of these, and set the del bool to whether you want the BigObjPtr to destroy its pointer when it goes out of scope:

    class abc
    {
    public:
        virtual BigObjPtr obj() = 0;
    };
    
    class derived : public abc
    {
    public:
        ...
        BigObjPtr obj() { return BigObjPtr(false, &obj_); }
    
    private:
        BigObj obj_;
    };
    
    class otherderived : public abc
    {
    public:
        ...
        BigObjPtr obj() { return BigObjPtr(true, new BigObj); }
    };
    

    You’d of course need to manage copying of BigObjPtrs, etc. but I leave that to you.

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

Sidebar

Related Questions

Lets say I have an abstract class Cat that has a few concrete subclasses
Lets say I have an abstract base class and a derived class with some
Let's say I have an abstract base class Base with a virtual function doSomething()
For simplicity's sake, lets say I have the following Abstract Base Controller Class: public
Let's say I have an abstract base class that looks like this: class StellarObject(BaseModel):
Lets say I have the following code: abstract class Animal case class Dog(name:String) extends
Lets say i have the following c# classes: abstract class a { protected abstract
Let's say I have a Java class abstract class Base { abstract void init();
Let's say I have this base class: abstract public class Base { abstract public
I have a base class and several concrete classes that derive from it. Lets

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.