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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:07:13+00:00 2026-05-16T16:07:13+00:00

I wish I could return an object of virtual base class so that I

  • 0

I wish I could return an object of virtual base class so that I won’t need to deal with the memory management(the idea of function programming also stimulate this pursuit). That means I am looking for some things like below:

class Car
{
  public:
     virtual int price() = 0 ;
     virtual string brand() = 0 ;
}

class Interface
{
  public:
     virtual Car giveMeACar() = 0 ;
     virtual vector<Car> listMeAllTheCars() = 0 ;
}

However, this won’t even compile due to that Car is an abstract interface, with an error message :

invalid abstract return type for
member function ‘virtual Car
giveMeACar() = 0
; because the
following virtual functions are pure
within ‘Car‘ : int price()
string brand() ;

So, does that means I have to revise the interface to something like below and manager the memory myself (delete the instance after using it) – ruling out the option of using smart pointer.

class Interface
{
  public:
     virtual Car* giveMeACar() = 0 ;
     virtual vector<Car*> listMeAllTheCars() = 0 ;
}

My question is : is this the only option I have when design an interface where every things(class) is abstract?

Returning an object of interface class is perfect valid in Java. C++ seems to be litter bit verbose and counter intuitive in this facet. More than often, I feel C++ is “pointer to object programming language” instead of a “object programming language” because without a pointer you can not get too much benefit of object programming.

  • 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-16T16:07:14+00:00Added an answer on May 16, 2026 at 4:07 pm

    In Java, “returning and object” is actually semantically equivalent to returning a pointer to the object in C++, you are trying to return an object by value, which makes a copy of it. You can’t make a copy of an abstract object.

    So, while C++ may be more verbose, it supports different semantics for passing parameters and returning values, which Java doesn’t support (return by value, pass by reference).

    With that said, you should return by smart pointer, which does memory management for you. Others have pointed out auto_ptr with ownership transfer semantics, but you can also use boost::shared_ptr, in case you use custom memory allocation internally (e.g. a pool), shared_ptr‘s custom deleter feature will help you hide deallocation details from the user of the interface. It can also be used in STL containers (unlike auto_ptr).

    class Car
    {
    public:
        typedef boost::shared_ptr<Car> Ptr;
        virtual int price() = 0 ;
        virtual string brand() = 0 ;
    };
    
    class Interface
    {
    public:
        virtual Car::Ptr giveMeACar() = 0 ;
        virtual vector<Car::Ptr> listMeAllTheCars() = 0 ;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a very large and mature C++ code base that I'm trying to
i wish to have an android gallery that will host images of varying aspect
I have an array a whose values I wish to modify through another function.
I have a large 1D dynamic array in my program that represents a FITS
I am using the following function within a WndProc override: Public Function GetPortName() As
I'm pretty new to jQuery and javaScript programming and wondered if anyone could help
I'm writing an application that validates login credentials on an external webserver - so
I've decided to remove some of the using statements in my code so I
Imagine a valid #RGB color in hexadecimal, defined as $color = "#f7b9a0"; Now I
I'm struggling with how to parse a response from an XMLHttpRequest. The response is

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.