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

  • Home
  • SEARCH
  • 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 8047309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:03:39+00:00 2026-06-05T06:03:39+00:00

i have a base class, 2 derived classes and an entitymanager class that has

  • 0

i have a base class, 2 derived classes and an entitymanager class that has a container of base pointers pointing to derived objects. i have a virtual clone method in the base to take care of the copy constructors in the derived classes, but im having trouble wrapping my head around overloading the assignment operator and preventing slicing, could someone please help with that and perhaps review how ive handled the entitymanager copy constructor? i think its ok

class System
{
public:
    virtual System* clone()=0;
};

class projectile :public System
{
public:
    projectile* clone()
    {
        return new projectile(*this);
    }
};

class player : public System
{
public:
     player* clone()
    {
        return new player(*this);
    }
};

class EntityManager
{
private:
    vector<System*> theEntities;
public:
    EntityManager(){}
    EntityManager(EntityManager& other)
    {
        for (size_t i=0;i<other.theEntities.size();i++)
            theEntities.push_back(other.theEntities[i]->clone());
    }
    void init()
    {
        projectile* aProjectile = new projectile;
        player* aPlayer = new player;
        theEntities.push_back(aProjectile);
        theEntities.push_back(aPlayer);
    }
};

int main (int argc, char * const argv[]) 
{
    EntityManager originalManager;
    originalManager.init();
    EntityManager copyManager(originalManager);

    return 0;
}
  • 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-05T06:03:39+00:00Added an answer on June 5, 2026 at 6:03 am

    Add a swap member that swaps the containers, then implement assignment as copy and swap:

    void swap(EntityManager& other)
    { theEntities.swap(other.theEntities); }
    
    EntityManager& operator=(EntityManager other)
    { swap(other); return *this; }
    

    The argument to the assignment operator will be copied using the copy constructor you’ve already written, then you swap the data, so the data that belonged to *this will be destroyed when that parameter goes out of scope, and *this owns the newly copied data.

    Re-using the copy constructor in this way means you only need to implement a correct copy constructor (and correct swap, which is usually easy to get right) and your assignment operator is really simple and automatically correct.

    N.B. your init member and copy ctor are not exception safe, if any push_back operation throws an exception you leak memory. You’re also missing a destructor, but I assume that’s present in the real code.

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

Sidebar

Related Questions

I have a base class that has a method that gets executed by derived
I have a base class with several derived classes that extend it. I want
I have base class BaseClass and derived classes DerivedA , DerivedB , and DerivedC
I have a base class Media and several derived classes, namely DVD , Book
I have class Base and classes Derived_1 , Derived_2 ... I need derived classes
I have a abstract base class A and a set of 10 derived classes.
I have these classes: class Base { ... private: std::vector<X> v; }; class Derived
I have a single class Base, and a few tens of classes derived from
I have a base class called Items and 3 derived classes, and within the
I have a base class and several derived classes. The derived classes use some

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.