-1

Say i have this class:

class Entity
{
private:
    std::vector<Component*> _components;
public
    Entity(std::vector<Component*>);
};

And i do this:

Entity* entity= new Entity( { new Drawable(), new Transform() ... } );
Entity* clone = new Entity(*entity);

Will the objects that the pointers in _components point to get copied? Or are only the pointers getting copied to clone?

ojoj kolol
  • 79
  • 6
  • 3
    Only the pointers in `_components` will be copied. You should follow the rule of three (or five) for your class to manage the memory correctly, see: https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three – Richard Critten Jul 23 '17 at 16:17
  • 1
    Pointers are getting copied, not objects pointed to. Write a custom copy constructor. – Jesper Juhl Jul 23 '17 at 16:17
  • 3
    I feel you don't really know when to use pointers and when not to. do you come from a Java/C# background? – David Haim Jul 23 '17 at 16:18
  • I am a student so i dont really have much of a background, the context that the components are used require them to be pointers, but i will review my design again. – ojoj kolol Jul 23 '17 at 18:12

1 Answers1

1

Will the objects that the pointers in _components point to get copied?

No.

Or are only the pointers getting copied to clone?

Yes.

user0042
  • 7,917
  • 3
  • 24
  • 39