As well as a class includes a default constructor and a copy constructor even if they are not declared, it also includes a default definition for the assignment operator (=) with the class itself as parameter. The behavior which is defined by default is to copy the whole content of the data members of the object passed as argument (the one at the right side of the sign) to the one at the left side. |
RyanM wrote: |
---|
"Can you please break it down and provide a few more details. How is "Point r(q)" equivalent to "r = q"?" |
|
|
|
|
only if you do something stupid |
|
|
|
|
Can you explain what a temporary is? Sorry. |
Point( 0, 0 );
Point(Point &o);
Point p1( Point( 0, 0 ) );
Point( const Point &o);
|
|
Also what is a regular reference? |
int const &X = 10;
The reference has to be constant, because you're assigning a literal to it.
|
|
A temporary object can be bound to const reference.
Point( const Point &o);
|
|
|
|
true, '1', L'1', u8'1', u'1', U'1', "1", L"1", u8"1", u"1", U"1", R("1"), 1, 1u, 1l, 1ul, 1ll, 1ull, 1.0f, 1.0, 1.0L
Point(1, 1), pair<int, int>(1, 1), vector<int>(2)
|
|
true
to false
!!Point( const Point &o);
|
|
The copy constructor by default doesn't copy the values from the temporary object to p1, but points to the temporary object instead |
This is a problem because like I said the temporary object will be destroyed |
Am I correct so far? |
Well how does adding const suddenly solve all that? The object is still temporary. |
Also, the following code will work, correct?
|
|
|
|
|
type name(objectOfType_type);
means:Viliml wrote: |
---|
copy-constructors should take a reference toa constant, to avoid changing the object from which the data is copied. This also allows constructon form temporaries, which isn't possible with regular references. |
Viliml also wrote: |
---|
can't be a regular reference because of an obvious reason: youcan't modify it! Temporarys are mostly, or maybe even only, literals and constructor results. They musn't be modified, because that would lead to unexpected results. Taking a CONSTANT reference allows correct code, and no undefined results. Another reson for taking a constant reference is not to accidentally modify the object you are copying, as it would mostly be unexpected. |
Viliml also wrote: |
---|
because, when you change the reference, you change the object it's reffering to. If the object it's reffering to is a constant, it musn't be changed, so common sense tells us you can't have a writable reference to a const object. |
Viliml ALSO wrote: |
---|
adding const means that you can't change it, which means that the temporary, possibly a literal, still contains the same value! Also this guarranties that it is nothing but a COPY! That means you can't accidentally chang the object you are copying from, because then when you would use it's value later, you would wonder and wornder how the heck did it's value change, if you did NOTHIGN to change it in the main(). It would be really hard to debug it when it's inside the copy-constructor. Adding const assures you it won't happen. |
this
pointer doen't point to it either.! The only relation to the temporary is the data!