Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Questions of copy constructor

Reply
Thread Tools

Questions of copy constructor

 
 
away
Guest
Posts: n/a
 
      09-14-2004
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!


 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      09-14-2004
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.

> 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
 
Reply With Quote
 
 
 
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      09-14-2004
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 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.


--
Karl Heinz Buchegger
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      09-14-2004

"away" <(E-Mail Removed)> wrote in message
news:B2D1d.364613$(E-Mail Removed)...
> 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


 
Reply With Quote
 
Niklas Borson
Guest
Posts: n/a
 
      09-14-2004
Victor Bazarov <(E-Mail Removed)> wrote in message news:<qgD1d.2030$(E-Mail Removed)09.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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
template copy constructor vs normal copy constructor cinsk C++ 35 10-10-2010 11:14 PM
A constructor calling another constructor (default constructor)? Generic Usenet Account C++ 10 11-28-2007 04:12 AM
Calling base class constructor from derived class Copy constructor ali C++ 4 03-05-2007 09:15 AM
deep/shallow copy - constructor v Object.copy() VisionSet Java 8 04-29-2004 10:41 PM
Copy constructor hides default constructor Aire C++ 3 01-25-2004 05:47 PM



Advertisments