constructors and copy constructors

Started by
13 comments, last by jpetrie 14 years, 11 months ago
My book reads that an object is copied automatically when it is initialized to another object through an initializer. I'm just not sure what this means. For starters, what is an initializer? Is that the same thing as a constructor or copy constructor? But then what does it mean by initialized to another object? Thank you in advance.
Advertisement
To "initialize," in this context, means something like "to bring into existence with a specific set of values."

When you initialize an object "to" another object, the first object comes into being with the values of the second object. This is distinctly different from if the first object were to come into being with some other set of values and for those values to later be changed to mirror the values of some other object. How those values get from the source object to the object being initialized depends on how the initialization is occuring and such.

An initializer is not the same thing as a constructor -- it's a more general term, although it is often used within or as part of constructors (see "initializer list").

Initialization is also occuring here:
int x = 32; // x is initialized to 32.

although no constructors are involved and this code could exist anywhere (not neccessarily in a constructor).
I totally agree with mr petrie here,
initialising means giving a starting value to anything(cud be structures,variables,objects etc)
but when it is said in context of objects, especially copying them(or initialising one with thhe values of another) ,doesn't that automatically mean we're talking about copy constructor?
Quote:Original post by Manpreet
but when it is said in context of objects, especially copying them(or initialising one with thhe values of another) ,doesn't that automatically mean we're talking about copy constructor?

1. You can copy user-defined types without invoking the copy constructor:
Foo a = b; // this calls the copy constructor    a = b; // this calls the copy assignment operator

2. According to the C++ standard, ints are also objects, and there are no constructors involved in copying an int.
Quote:Original post by DevFred
1. You can copy user-defined types without invoking the copy constructor:Foo a = b; // this calls the copy constructor    a = b; // this calls the copy assignment operator



Yes Yes its all coming back!!

I read this in a book 3 months back that you can copy values of an object to another by directly using the '=' operator "BUT IT SHOULD NOT BE USED WHEN THE CLASS DECLARATION HAS A POINTER AS A DATA MEMBER!"

Thanx for waking me up :D

Articles on cplusplus are usually quite informative and explanatory. http://cplusplus.com/forum/beginner/6725/
Quote:Original post by Manpreet
Quote:Original post by DevFred
1. You can copy user-defined types without invoking the copy constructor:Foo a = b; // this calls the copy constructor    a = b; // this calls the copy assignment operator



Yes Yes its all coming back!!

I read this in a book 3 months back that you can copy values of an object to another by directly using the '=' operator "BUT IT SHOULD NOT BE USED WHEN THE CLASS DECLARATION HAS A POINTER AS A DATA MEMBER!"

Thanx for waking me up :D


No, if the class has a pointer as a member and copying the pointer when copying the object is not the correct behavior (which is very often the case), you should write your own operator= and copy constructor.
Quote:Original post by Manpreet
you can copy values of an object to another by directly using the '=' operator
"BUT IT SHOULD NOT BE USED WHEN THE CLASS DECLARATION HAS A POINTER AS A DATA MEMBER!"

Sure it can be used. You just have to follow the "rule of three", which means you have to implement the copy constructor, the copy assignment operator and the destructor yourself.
what is the copy assignment operator? is that:
void operator = (ThisClass bla);

or is it something else?
Quote:Original post by MadMax1992
what is the copy assignment operator? is that:
void operator = (ThisClass bla);

or is it something else?

Close:
ThisClass& operator= (const ThisClass& other);


EDIT: mistake in the return type

This topic is closed to new replies.

Advertisement