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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T08:36:57+00:00 2026-05-12T08:36:57+00:00

I can’t figure out what is up with this. I have a Scene class

  • 0

I can’t figure out what is up with this.

I have a Scene class that has a vector of Entities and allows you to add and get Entities from the scene:

class Scene {
    private:
        // -- PRIVATE DATA ------
        vector<Entity> entityList;
    public:
        // -- STRUCTORS ---------
        Scene();
        // -- PUBLIC METHODS ----
        void addEntity(Entity); // Add entity to list
        Entity getEntity(int); // Get entity from list
        int entityCount();
};

My Entity class is as follows (output is for testing):

class Entity {
    public:
        virtual void draw() { cout << "No" << endl; };
};

And then I have a Polygon class that inherits from Entity:

class Polygon: public Entity
{
    private:
        // -- PRIVATE DATA ------
        vector<Point2D> vertexList; // List of vertices
    public:
        // -- STRUCTORS ---------
        Polygon() {}; // Default constructor
        Polygon(vector<Point2D>); // Declare polygon by points
        // -- PUBLIC METHODS ----
        int vertexCount(); // Return number of vertices
        void addVertex(Point2D); // Add vertex
        void draw() { cout << "Yes" << endl; }; // Draw polygon
        // -- ACCESSORS ---------
        Point2D getVertex(int); // Return vertex
};

As you can see, it has a draw() method that should override the draw() method it inherits from the Entity class.

But it doesn’t. When using the following code:

scene->getEntity(0).draw();

where entity 0 is a Polygon (or at least should be), it prints “No” from the parent method (as though it’s not a Polygon, just an Entity). In fact, it doesn’t seem to let me call any methods unique to Polygon without getting:

‘some method name‘ : is not a member of ‘Entity’

So any idea what’s up?

Thanks for the help.

UPDATE:

So I’ve implemented the code given in the first answer, but I’m not sure how to add my polygon to the list. Something like this?

const tr1::shared_ptr<Entity>& poly = new Polygon;
poly->addVertex(Point2D(100,100));
poly->addVertex(Point2D(100,200));
poly->addVertex(Point2D(200,200));
poly->addVertex(Point2D(200,100));
scene->addEntity(poly);

I’m just not used to this shared_ptr business.

  • 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-12T08:36:57+00:00Added an answer on May 12, 2026 at 8:36 am

    I think that you need to post your calling code, but the essentially problem is this.

    You have a concrete class Polygon deriving from another concrete class Entity. Your addEntity and getEntity functions take and return an Entity by value so if you try to pass in or retrieve an Entity, you will copy only the Entity part of that object (slicing it) and the information about the derived part of the object will be lost.

    In addition you have a vector of Entity, which is a vector of base class objects, so you have no way of storing anything other than the base type of object.

    If you need to have a collection of a mixed type of objects, but all derived from Entity, you may need to use dynamically created objects and some sort of smart pointer such as a tr1::shared_ptr or a boost::shared_ptr.

    E.g.

    class Scene {
        private:
            // -- PRIVATE DATA ------
            vector< std::tr1::shared_ptr<Entity> > entityList;
        public:
            // -- STRUCTORS ---------
            Scene();
            // -- PUBLIC METHODS ----
            void addEntity( const std::tr1::shared_ptr<Entity>& ); // Add entity to list
            const std::tr1::shared_ptr<Entity> getEntity(int); // Get entity from list
            int entityCount();
    };
    

    Edit

    Your updated calling code is essentially correct, although using a local const reference to a shared pointer is a bit obscure.

    I’d probably go with something like:

    std::tr1::shared_ptr<Polygon> poly( new Polygon );
    poly->addVertex(Point2D(100,100));
    poly->addVertex(Point2D(100,200));
    poly->addVertex(Point2D(200,200));
    poly->addVertex(Point2D(200,100));
    scene->addEntity(poly);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can I have a project that has some parts written in c and other
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Can find why i get this error can someone help? package Android.data; public class
Can someone maybe tell me why this is not working? I have used echo
Can any one help me in sorting this out in sed/awk/perl Input file Start
Can anyone help me trying to find out why this doesn't work. The brushes
Can't get over it, no matter how much I search. I have AlarmManager, where
Can some one help or explain to me how this works please? i have
Can someone give me a hand with this please? I have been using jquery-validate
Can I order my users in the database, so I don't have to say

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.