My problem is that the default copy constr. messes up my pointers.
Simplified version:
-
I have a
Aclass with the default copy constr. -
Ahas 2 member objects:BandC. -
Bhas a pointer toC.
Situation:
-
I store
Ain astd::vector:vector.emplace_back(A(...)); -
emplace_back does this:
-
Creating A (setting up B's pointer to C, etc)
-
Copying A into the vector. (default copy ctr: copies by value)
-
Destroying the "origin" A.
-
. Result:
- The storage has an
Awhich has aBwhich has a pointer to the old A's C. Which doesn't exist anymore.
Simple solution would be:
- In
Athere would be pointers toBandC
But this is not so good because why should A have pointers, when it owns its B and C and B and C shares lifetime with A.
Question:
-
Isn't there a better way than having pointers in A?
-
Or isn't there a simplier way than messing with copy constructor implementations?
-
I thought that emplace constructs "in-place", so why does it need a copy ctr?
Aucun commentaire:
Enregistrer un commentaire