Questions of copy constructor

A

away

1. When a class defined with a member of pointer type, it's necessary to
have a copy constructor and assignment operator.

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?

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?

3. If such a pointer member of pointer type of "void", such void type can be
casted to any other type at runtime, how to code a copy constructor for such
a member?

Thanks for help!
 
V

Victor Bazarov

away said:
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.
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).
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.
3. If such a pointer member of pointer type of "void", such void type can be
casted to any other type at runtime, how to code a copy constructor for such
a member?

One should never use such mechanism. Rethink your design.

V
 
K

Karl Heinz Buchegger

away said:
1. When a class defined with a member of pointer type, it's necessary to
have a copy constructor and assignment operator.

Depends if this pointer is an owning pointer or not.
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?

No. But you could do:
Declare the copy constructor and assignement operator private and don't implement
them. Then the compiler and/or linker will guard you if you make the mistake of
violating your rule.
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?

If the derived class on its own doesn't need a copy constructor/op= then
no. You don't have to do anything. The compiler generated ones will do
the right thing.
3. If such a pointer member of pointer type of "void", such void type can be
casted to any other type at runtime, how to code a copy constructor for such
a member?

Bad idea. You shouldn't do this in the first place.
 
J

John Harrison

away said:
1. When a class defined with a member of pointer type, it's necessary to
have a copy constructor and assignment operator.

Not always true. It's only necessary to have a copy constructor if the
compiler generated one does the wrong thing. Having a pointer doesn't
necessarily mean the compiler generated copy constructor is wrong.

A better rule is the rule of three. If your class has a destructor or a copy
constructor or an assignment operator then it will probably need all three.
So for instance if you have a pointer in a class AND you delete that pointer
in the destructor then you are going to need a copy constructor and
assignment operator.
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?

No they aren't. But for safety's sake in this case you should declare the
copy constructor and assignment operator as unimplemented private methods.
This will prevent them being called accidentally.
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?

No. It's the same rule as above. If the derived class (only) has a
destructor or an assignment operator or a copy constructor then its probably
going to need all three.
3. If such a pointer member of pointer type of "void", such void type can be
casted to any other type at runtime, how to code a copy constructor for such
a member?

There's no answer to that. It depends.

john
 
N

Niklas Borson

Victor Bazarov said:
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.
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.
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top