473,406 Members | 2,390 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,406 software developers and data experts.

Default Copy Constructor

Bob
Hi,

If a class contains only built-in types and standard library objects
such as strings and vectors, is it safe to let the compiler create the
copy constructor and assignment operator, or is it advised to always
write your own?

I know you need to write your own if there is any dynamic memory
allocation etc, but I was curious as to the "correct" way in the case
of static variables.

Thanks,
Bob.
Jul 22 '05 #1
3 1703
"Bob" <bo******@hotmail.com> wrote in message
news:77*************************@posting.google.co m
Hi,

If a class contains only built-in types and standard library objects
such as strings and vectors, is it safe to let the compiler create the
copy constructor and assignment operator
Yes.
or is it advised to always
write your own?


No. In fact it is safer to let the compiler do it since you might make a
mistake.

There are occasions when you might need to create your own versions (e.g.,
if you want to keep count of the number of objects created of a particular
class) but, unless you want to do something beyond mere construction or
assignment, the compiler-generated version is fine.
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Jul 22 '05 #2
Bob wrote in news:77*************************@posting.google.co m in
comp.lang.c++:
Hi,

If a class contains only built-in types and standard library objects
such as strings and vectors, is it safe to let the compiler create the
copy constructor and assignment operator, or is it advised to always
write your own?

The rule is quite simple:

If all members have constructors, assigment operator and destructor that
"do the right thing(tm)" then the compiler generated ctor's etc
will do the right thing.

Typically pointers that point to dynamic memory don't obey this rule.
Another example would be the FILE * returned by fopen (it needs to
be closed by fclose).

Most of the types defined by the C++ part of the standard library
do obey this rule, however ther are some oddity's:

- std::auto_ptr< T > - it has strange copying behaviour (it moves).
- std::ostream et al - doesen't provide for copying (ctor or op = ).
I know you need to write your own if there is any dynamic memory
allocation etc, but I was curious as to the "correct" way in the case
of static variables.


static variables aren't part of an instance, they're globals (but globals
that are restricted to the classes scope) and don't need to be constructed
by the copy constructor or assigned to by the assignment operator.
Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #3
"John Carson" <do***********@datafast.net.au> wrote...
"Bob" <bo******@hotmail.com> wrote in message
news:77*************************@posting.google.co m
Hi,

If a class contains only built-in types and standard library objects
such as strings and vectors, is it safe to let the compiler create the
copy constructor and assignment operator


Yes.


Well, to be technically correct, _pointers_ _are_ in fact built-in types,
but somehow I think the OP didn't mean pointers. In any case, mentioning
"The Rule of Three" could also help.

V
Jul 22 '05 #4

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

Similar topics

15
by: A | last post by:
Hi, A default copy constructor is created for you when you don't specify one yourself. In such case, the default copy constructor will simply do a bitwise copy for primitives (including...
12
by: Marcelo Pinto | last post by:
Hi all, In practice, what is the diference between a default constructor and an explicit default constructor? class Ai { public: Ai() {} };
22
by: sujilc | last post by:
This question seems to be silly. Can ony one figure out the default functions implemented by compiler when we decalre a class like class A { } According to me this declaration will define...
74
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the...
23
by: Jess | last post by:
Hello, I understand the default-initialization happens if we don't initialize an object explicitly. I think for an object of a class type, the value is determined by the constructor, and for...
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...
43
by: JohnQ | last post by:
Are a default constructor, destructor, copy constructor and assignment operator generated by the compiler for a struct if they are not explicitely defined? I think the answer is yes, because...
3
by: subramanian100in | last post by:
If we provide any ctor for a class the compiler does not supply the default ctor. However if we do not provide the copy ctor but provide any other ctor, the compiler still supplies the copy ctor....
10
by: JosephLee | last post by:
In Inside C++ object Model, Lippman said there are four cases in which compile will sythesize a default constructor to initialize the member variables if the constructor is absent: 1. there is a...
9
by: puzzlecracker | last post by:
From my understanding, if you declare any sort of constructors, (excluding copy ctor), the default will not be included by default. Is this correct? class Foo{ public: Foo(int); // no...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.