Re: Copy Constructors and Assignment Operator, When should I use?
Sorry my code example has a lot of typos. I extract my current code
and changed those classes names.
So from what you wrote:
No. It works for any datatype that defines its own copy constructor
that you don't have to invoke explicitly, like any standard container,
strings, any POD, any classes containing those (which is actually
kind of recursive).
ItemSet set1;
set1.load();
ItemSet set2;
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?),
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?
------------------------------------------------------
struct ItemA{
int aInt;
std::string aString;
} ;
struct ItemB{
int bInt;
std::string bString;
time_t bTime;
} ;
class ItemSet{
ItemListA a;
ItemListB b;
void load(){
a.load():
b.load();
}
};
class ItemListA{
std::vector <ItemA> m_vecA;
void load(); //function for populate m_vecA
ItemA FindItem(){
ItemA ia;
return ia;
}
};
class ItemListB{
std::vector <ItemB> m_vecB;
void load(); //function for populate m_vecB
//other functions are not shown here
};
--------------------------------
On Feb 29, 5:06 pm, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
rockdale wrote:
I know this is a basic concept but I still need some clearification.
It gave me a hard time
I read something says "Don't write a copy constructor if shallow
copies are ok" and "If you need a copy constructor, you also need a
destructor and operator="
The latter is what's commonly knows as "the Rule of Three", and in
most cases it relates to dynamic memory management.
My understanding, the shallow copy work for class that with int,
float ...etc internal datatype only.
No. It works for any datatype that defines its own copy constructor
that you don't have to invoke explicitly, like any standard container,
strings, any POD, any classes containing those (which is actually
kind of recursive).
If I have pointers or vectors, is
that means I need to write my Copy Constructor/Operation operator.
Pointers - you need to be specific. Vectors, no, you don't, usually.
Nothing clearer than an example.
Let's say I have a class:
Class ItemSet{
You mean
class ItemSet{
?
ItemListA a;
ItemListB b;
Supposedly, those types have been defined...
load() // call ItemListA.load(), call ItemListB.load()
You mean,
void load();
?
}
;
Class ItemListA{
You mean
class ItemListA{
? Make sure this is defined before it's used.
struct ItemA{
int aInt;
std::string aString;
} ;
std::vector <ItemA> m_vecA;
void load(); //function for populate m_vecA
ItemA FindItem(){
ItemA ia;
return ia
}
}
Semicolons are commonly AWOL from your code, eh?
Class ItemListB{
You mean
class ItemListB{
?
struct ItemB{
int bInt;
std::string bString;
time_t: bTime;
What's the colon doing after 'time_t'?
} ;
std::vector <ItemB> m_vecB;
load(); //function for populate m_vecB
}
----------------------------------
Now Can I do:
ItemSet set1;
set1.Load();
You can't. The member function name is 'load', not 'Load'.
set1.load();
ItemSet set2;
set2 = set1
? without create copy constructors for ItemSet class?
The compiler provides the copy constructor and the copy assignment
operator if you don't declare yours.
If I need to create copy constructor for ItemSet class, since
ItemListA and ItemListB is in ItemSet, I also need to create copy
constructor for these 2 classes, right/
No. Every class has a compiler-provided copy constructor and copy
assignment operator; if the compiler can do it for you, it will.
-----------------------------
Also, if I need to create the copy constructor /overwrite assignment
operator for Item:
why does the function in ItemListA which returns struct ItemA work? Is
the return COPY the ItemA ?
I am not sure I understand the questions.
What book are you reading that does not explain what functions are
provided by the compiler?
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask