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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:56:51+00:00 2026-05-14T15:56:51+00:00

Here is the question’s plot: suppose I have some abstract classes for objects, let’s

  • 0

Here is the question’s plot: suppose I have some abstract classes for objects, let’s call it Object. It’s definition would include 2D position and dimensions. Let it also have some virtual void Render(Backend& backend) const = 0 method used for rendering.

Now I specialize my inheritance tree and add Rectangle and Ellipse class. Guess they won’t have their own properties, but they will have their own virtual void Render method. Let’s say I implemented these methods, so that Render for Rectangle actually draws some rectangle, and the same for ellipse.

Now, I add some object called Plane, which is defined as class Plane : public Rectangle and has a private member of std::vector<Object*> plane_objects;

Right after that I add a method to add some object to my plane.

And here comes the question. If I design this method as void AddObject(Object& object) I would face trouble like I won’t be able to call virtual functions, because I would have to do something like plane_objects.push_back(new Object(object)); and this should be push_back(new Rectangle(object)) for rectangles and new Circle(...) for circles.

If I implement this method as void AddObject(Object* object), it looks good, but then somewhere else this means making call like plane.AddObject(new Rectangle(params)); and this is generally a mess because then it’s not clear which part of my program should free the allocated memory.

[“when destroying the plane? why? are we sure that calls to AddObject were only done as AddObject(new something).]

I guess the problems caused by using the second approach could be solved using smart pointers, but I am sure there have to be something better.

Any ideas?

  • 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-14T15:56:51+00:00Added an answer on May 14, 2026 at 3:56 pm

    Your actual problem seems to be managing the objects’ lifetimes. Four possibilities that come to mind are:

    1. Your container (i.e. Plane) assumes ownership of all contained objects and therefore deletes them once it’s itself destroyed.

    2. Your container (Plane) does not assume ownership and whoever added objects to your container will be responsible for destroying them.

    3. The lifetime of your objects is managed automatically.

    4. You circumvent the problem by providing the container with a clone of the actual object. The container manages a copy of the object, and the caller manages the original object.

    What you currently have seems like approach #4. By doing:

    plane_objects.push_back(new Object(object));
    

    you insert a copy of the object into the container. Therefore the problem sort of disappears. Ask yourself whether this is really what you want, or if one of the above choices would be more suitable.


    Options #1 and #2 are easy to implement, because they define a contract that your implementation simply has to follow. Option #3 would call for e.g. smart pointers, or some other solution involving reference counting.

    If you want to keep following the approach of option #4, you could e.g. extend your Object class with a clone method, so that it returns the right type of object. This would get rid of the incorrect new Object(...).

    class Object
    {
        public:
            virtual Object* clone() const = 0;
            ...
    };
    
    ...
    
    Object* Rectangle::clone() const
    {
        return new Rectangle(*this);  // e.g. use copy c'tor to return a clone
    }
    

    P.S.: Note how the STL containers seem to deal with this issue: Let’s say you declare a vector<Foo>. This vector will contain copies of the objects inserted into it (option #4 in my answer). However, if you declare the collection as vector<Foo*>, it will contain references to the original objects, but it will not manage their lifetime (option #2 in my answer).

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

Sidebar

Ask A Question

Stats

  • Questions 386k
  • Answers 386k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer See demo here :) You can simply do like: <style… May 14, 2026 at 11:51 pm
  • Editorial Team
    Editorial Team added an answer I'll try to explain: There is an application, with thrift… May 14, 2026 at 11:51 pm
  • Editorial Team
    Editorial Team added an answer Should I be storing the Instance references in a structure… May 14, 2026 at 11:51 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.