Re: Copy Constructors and Assignment Operator, When should I use?
- From: "Alex Blekhman" <tkfx.REMOVE@xxxxxxxxx>
- Date: Mon, 3 Mar 2008 13:07:12 +0200
"rockdale" wrote:
[...]
I simply assign set1 to set2 like this
set2 = set1;
will this assignment will copy whatever in set1 to set2? (deep
copy,
even the vector items?),
Yes, it will copy everything, provided that set items have proper
copy constructor. The `std::vector' container can copy itself
without a problem.
well my coworker told me that I need to have a define my own
copy
constructor for ItemSet, ItemListA and ItemListB for it works
properly. in the copy contructor of ItemSet,
ItemSet(ItemSet& toCopy){
//copy constructor
a = toCopy.a;
b = toCopy.b;
}
notice a and b are classes that I defined, to enable a =
toCopy.a
works properly. I need to define copy contructor in my ItemListA
and
ItelListB classes, basically, to manually the vector from the
toCopy,
is his statement true?
[...]
No. Either he misunderstood your design or plain wrong about copy
constructors.
struct ItemA{
int aInt;
std::string aString;
} ;
struct ItemB{
int bInt;
std::string bString;
time_t bTime;
} ;
Both `ItemA' and `ItemB' classes don't need explicit copy
constructor since compiler generated cctor is good enough. The
`std::string' container knows how to copy itself and other types
are just fine with language provided copy.
class ItemSet{
ItemListA a;
ItemListB b;
...
};
The `ItemSet' class doesn't need cctor either since all its
members know how to copy themselves.
class ItemListA{
std::vector <ItemA> m_vecA;
...
};
class ItemListB{
std::vector <ItemB> m_vecB;
...
};
Both `ItemListA' and `ItemListB' classes don't need explicit copy
constructor since their only memebr is `std::vector', which has
proper cctor.
HTH
Alex
.
- Follow-Ups:
- References:
- Prev by Date: Re: 0xc0000142 Crash in VS2005 (C/C++ App) NTDLL Symbols Not Loading..
- Next by Date: Re: class type initialization
- Previous by thread: Re: Copy Constructors and Assignment Operator, When should I use?
- Next by thread: Re: Copy Constructors and Assignment Operator, When should I use?
- Index(es):
Relevant Pages
|