473,748 Members | 2,471 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1809
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*******@spam bs.com> wrote in message
news:B2******** *************@b gtnsc05-news.ops.worldn et.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.********@com Acast.net> wrote in message news:<qg******* ********@newsre ad1.dllstx09.us .to.verio.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
5799
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 kind. It sounds simple but evidently .NET has difficulty with this concept for some reason. I do understand that .NET objects are created on the GC heap but that doesn't mean that they couldn't be copied from another object of the same kind when...
3
1237
by: Birt | last post by:
1. class Engine; class Car { Engine& e; .... }; How to initialize a reference member, e?
14
1984
by: MSR | last post by:
I have a couple of questions. 1. Copy Constructor. class A { private: int a1; double d1; char *ptr;
12
1470
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 between public and private inheritance (other than the obvious one)? Thanks, Sashi
25
2782
by: Syam | last post by:
Fully answered interview questions in c and c++ from http://www.faiqs.com
4
3712
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 value-initialization. I think default-init calls default constructor for class objects and sets garbage values to PODs. Value-init also calls default constructor for class objects and sets 0s to POD types. This is what I've learned from the books (especially...
13
1692
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
1686
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), istream_iterator<string>(), back_inserter(v));
1
2776
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 objects. My goal here is to abstract away the database part, so that in the future these objects could be constructed from a data source on disk, across the network, etc. The interface I'm after is similar to the standard iterator interface for an...
0
9530
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9363
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9312
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9238
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8237
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2775
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2206
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.