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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:03:59+00:00 2026-06-12T23:03:59+00:00

I’m programming a basic graphic scene editor with OpenGL and C++, and this is

  • 0

I’m programming a basic graphic scene editor with OpenGL and C++, and this is the context:

  • I have a Drawable class, which is abstract and has only one pure virtual function draw(). I’m using this class like a java interface.

  • Many classes that inherit from Drawable and reimplement draw().

  • A Scene class which contains a list of pointers to Drawables.

  • The main.cpp where I create that differents Drawable objects and add them to the list.

I’m having this issues:

  • Created objects in main.cpp constantly going out of scope, so it’s not just easy as creating them and executing the adding function with a referenced object. (Just keeping a list with no valid pointers all the time).

  • A bittersweet solution is creating these new objects with new and letting the Scene class deleting the pointers in the list when it is destroyed, or deleting them in main.cpp somehow.

I don’t really like this so I want to ask if there is some method to copy the objects inside the add function and then store the copy in the list, which it’s not a problem because the copied object it’s going to erase soon. Inside that function I don’t know which subclass of Drawable I’m attending. I can’t make a copy directly of the Drawable object because Drawable is an abstract class, can’t make Drawable objects with new for the same. Just want to have a list with different objects which all can execute draw(). I leaving some code just in case:

class Drawable {
public:
    virtual void draw() = 0;

    virtual ~Drawable() = 0 {}
};


class Figure : public Drawable {
private:
    list<Point, allocator<Point>> _points;
    int _type;

public:
    ...

    void draw() {
        ...
    }   
};


class Scene : public Drawable {
private:
    list<Drawable*, allocator<Drawable*>> _drawables;
    ...

public:
    ...

    void add(Drawable* drawable) {
        _drawables.push_back(drawable);
    }

    ~Scene() {
        for(iterDrawable it = _drawables.begin(); it != _drawables.end(); ++it)
        delete (*it);
    }

    void draw() {
        for(iterDrawable it = _drawables.begin(); it != _drawables.end(); ++it)
            (*it)->draw();
    }   
};


main.cpp
...
void display() {
    ...

    Figure* square = new Figure(GL_POLYGON);

    square->add(Point(xSquare, ySquare));
    square->add(Point(xSquare + squareWidth, ySquare));
    square->add(Point(xSquare + squareWidth, ySquare + squareHeight));
    square->add(Point(xSquare, ySquare + squareHeight));

    cScene.add(square);
    cScene.draw();

    ...
}
...

I hope I explained sufficiently.

  • 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-12T23:04:00+00:00Added an answer on June 12, 2026 at 11:04 pm
    1. Drawable must have a virtual destructor.
    2. You must have some kind of object ownership policy in place. In other words, for each and every object you allocate, you must have a very clear idea about disposing of that object. That’s how things are done in C++, as opposed to Java. When you allocate an object for adding to a scene, does the scene object become the ultimate owner of the component? If it does, then the scene has to delete the list of its components before it itself goes away (that is, in the destructor). If the scene object is not the owner, and merely a user, then who is the owner? Can you guarantee that the owner will outlive all users? If not, how the owner will pass ownership to the next owner? If you are going to copy an object, you must decide this question for the original and for the copy.
    3. Fancy stuff like shared_ptr and ptr_list are fine and dandy, but I suggest learning the basics first. It is important to understand why shared_ptr and ptr_list are needed, which problems they purport to solve, and what their solutions look like from 30000 feet. The best way to acquire such understanding is by stumbling upon, and trying to solve, some of these problems.
    4. Drawable must have a virtual destructor.
    5. If you want to copy objects polymorphically, then you need a polymorphic function that makes a copy. You have to write one yourself for each derived class, just like you write draw() for each derived class. The commonly accepted name for such a function is clone. For each class Foo, clone would look like this:

      virtual Foo* clone() {
          return new Foo(*this);
      }

      Versions of clone look almost identically for all classes, nevertheless you do have to write each one of them manually. Note that this is using Foo‘s copy constructor, which you will have to write yourself if Foo owns any other object, or has a copy operation that is non-trivial in some other way.

    6. Did I mention that Drawable must have a virtual destructor?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have an array which has BIG numbers and small numbers in it. I
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have an autohotkey script which looks up a word in a bilingual dictionary
This could be a duplicate question, but I have no idea what search terms
I have a text area in my form which accepts all possible characters from
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

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.