473,387 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Questions of copy constructor

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!
Jul 22 '05 #1
4 1779
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
Jul 22 '05 #2
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
kb******@gascad.at
Jul 22 '05 #3

"away" <Gu*******@spambs.com> wrote in message
news:B2*********************@bgtnsc05-news.ops.worldnet.att.net...
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
Jul 22 '05 #4
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.
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
3
by: Birt | last post by:
1. class Engine; class Car { Engine& e; .... }; How to initialize a reference member, e?
14
by: MSR | last post by:
I have a couple of questions. 1. Copy Constructor. class A { private: int a1; double d1; char *ptr;
12
by: Sashi | last post by:
All, I had an interview today and I couldn't answer these questions. Any good answers? Why does a copy constructor param need to be const? Ever need a virtual constructor? Major difference...
25
by: Syam | last post by:
Fully answered interview questions in c and c++ from http://www.faiqs.com
4
by: Jess | last post by:
Hello, I tried several books to find out the details of object initialization. Unfortunately, I'm still confused by two specific concepts, namely default-initialization and...
13
by: Jess | last post by:
Hello, I have some questions to do with dynamic binding. The example program is: #include<iostream> using namespace std; class A{
10
by: Jess | last post by:
Hello, When I copy contents from an input string to a vector, I typically use something like this vector<stringv; ifstream in("file"); copy(istream_iterator<string(in),...
1
by: Scott Gifford | last post by:
Hello, I'm working on an providing an iterator interface to a database. The basic thing I'm trying to accomplish is to have my iterator read rows from the database and return constructed...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.