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. 3 1722
"Bob" <bo******@hotma il.com> wrote in message
news:77******** *************** **@posting.goog le.com 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)
Bob wrote in news:77******** *************** **@posting.goog le.com 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/
"John Carson" <do***********@ datafast.net.au > wrote... "Bob" <bo******@hotma il.com> wrote in message news:77******** *************** **@posting.goog le.com 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 pointers) and for objects types call
their default constructor.
Any others points i should know?
|
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() {}
};
|
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 default functions like
|
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 creation of this
implicit default constructor, to force the creation of a struct via my
constructor only?
Zytan
|
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 the built-in types,
the value is usually garbage. Is this right?
However, I'm a bit confused about value-initialization, when does it
happen, and what kind of values are assigned to objects?
| |
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...
|
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 "there is no difference between a struct
and a class except the public/private access specification" (and a few minor
other things). When I create a class, I always start by declaring the
default constructor, copy constructor and assignment operator...
|
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.
Why doesn't the compiler supply the default ctor but still supplies
the copy ctor when we have defined any other ctor ?
Kindly explain
|
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 virtual function;
2. virtual inheritance;
3.base class with explicit default constructor;
|
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 Foo() is included, i believe.
};
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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,...
|
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...
|
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...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |