Victor Bazarov <v.********@comAcast.net> wrote in message news:<qg***************@newsread1.dllstx09.us.to.v erio.net>...
away wrote: 1. When a class defined with a member of pointer type, it's necessary to
have a copy constructor and assignment operator.
Depends on what the pointer points to.
To clarify, the usual reason for implementing a copy constructor and
assignment operator is that a class manages or "owns" some resource.
Such classes often contain pointers, but the presence of a pointer
is not a reliable guideline. A better guideline is whether a class
requires a non-trivial destructor.
Google or search the comp.lang.c++ FAQ for "rule of three"; in short,
if a class requires a destructor, copy constructor, or assignment
operator then it probably requires all three of them.
If don't pass objects of such class as value in a function and don't do
assignment, should copy constructor and assignment operator be unnecessary?
Depends on existence of other operations that may require copy
construction (like storing in a standard container).
Objects can be copied unintentionally, resulting in subtle bugs;
for example, due to implicit conversions. You can prevent this by
declaring a private copy constructor and assignment operator, and
not defining them.
2. If a base class have a pointer member, should its derived classes also
need copy constructor and assignment operator, no matter if a derived class
itself has a pointer member or not?
Probably not, depends on the derived class.
Right. If you don't define a copy constructor the compiler will
generate one that performs a member-by-member copy; similarly for
assignment.
Thus if you have a class X with a member of type std::string and
no user-defined copy ctor, and you copy one X to another, the
std::string copy ctor will be used to copy the string member.