2

i have the class

class Circle : public isCircle{
   private :
   int x;
   int y;
   vector<Circle*> _neighbors;
}

where isCircle is just an interface (with virtual methods), and the Circles which _neighbors contains pointers to weren't allocated by this instance. my question is if in this case the default copy and assignment operator would basically do a deep copy ?

hmjd
  • 120,187
  • 20
  • 207
  • 252
oopsilon
  • 81
  • 2
  • 5
  • 7
    A deep copy on pointers, yes, but not what the pointers point to. – ildjarn Feb 20 '12 at 20:33
  • And don't use names with "_" in front. – Dmitriy Kachko Feb 20 '12 at 20:36
  • 2
    @DmitryKachko you should keep subjective opinions to yourself if you're not going to provide arguments. – Luchian Grigore Feb 20 '12 at 20:39
  • Many languages and libraries have convention to use in front it for system variables, global constants etc. so just bad habit. Many styleguides prohibit that kind of use at all, so better not to have that habit. And as for my subject opinion, it's just bad readability at all, code should be readable. What's difference between _a and __a? Why I should count underscores while I read and revise such a code? )) – Dmitriy Kachko Feb 20 '12 at 20:43
  • 2
    @DmitryKachko: The main difference between `_a` and `__a` is that the second is reserved for any use, so you should never see it in user code (and can legitimately complain about it if you do). The first is only reserved as a name in the global namespace, so it's purely a matter of taste whether to use it in other contexts. – Mike Seymour Feb 20 '12 at 20:49
  • Oh, I should see it and make reports, unfortunately. – Dmitriy Kachko Feb 20 '12 at 20:53
  • @DmitryKachko see http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier – Seth Carnegie Feb 20 '12 at 20:56
  • @SethCarnegie Thank you ! What I'm talking about is "a personal rule" which is paercebal put as an answer at your link, and I agree with him absolutely. – Dmitriy Kachko Feb 20 '12 at 21:08
  • 3
    @DmitryKachko it's good for you to have a personal rule, but try to make sure to mention that it is just a personal rule when you tell it to others so that they can decide for themselves whether or not they want to follow the rule too. I understand though. – Seth Carnegie Feb 20 '12 at 21:12

2 Answers2

9

The default copy constructor for a C++ type works by invoking the copy constructor on each field in the instance with the corresponding field in the object the copy is being created from. In your example it roughly translates to

Circle(const Circle& other) :
  x(other.x),
  y(other.y),
  _neighobrs(other._neighbors) {

}

Whether or not the copy is deep is an implementation detail of each fields' copy constructor. In this case the copy constructor of vector<T> is a bit of a mix. It will deeply copy the underlying storage such that each vector<T> has it's own independent array. However it will copy the elements using the copy constructor. In this case it's a pointer type hence they are copied in a shallow fashion

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
0

Default generated copy constructors and assignment operators do shallow copies.

If you're sharing pointers between instances of these class, I suggest that instead of having a vector of pointers, you have a vector of smart pointers.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625