Compiler supplied assignment operator and references

B

blangela

What does the default assignment operator (compiler supplied assignment
operator, sometimes also referred to as the implicit assignment
operator or the synthesized assignment operator) do when the class
contains a reference. For example if the iref member of object A
references X and the iref member of B references Y, if we then go:

A = B;

will the iref member of A now reference X or Y.

If yes, then by my understanding of the term "shallow copy", a shallow
copy has occurred.

If instead, the iref member of A still references X and in fact Y's
value has been assigned to X, then by my understanding of the term, a
"deep copy" has occurred.

If the compiler does simple bitwise copy, then I would guess the
shallow copy is performed. If the complier actually does a memberwise
assignment of each member of the class, then a deep copy is likely
performed.

Also, can I assume that whatever the answer is for above, will be the
same for the compiler supplied copy constructor. For example:

MyClass A(B); // invokes default copy ctor

I realize this would be easy to confirm with some simple code, but I
don't have access to a C++ compiler at the moment.

Cheers,

Bob L.
 
A

Alf P. Steinbach

* blangela:
What does the default assignment operator (compiler supplied assignment
operator, sometimes also referred to as the implicit assignment
operator or the synthesized assignment operator) do when the class
contains a reference.

It's not generated in that case, because it can't be implemented.


[snip]
Also, can I assume that whatever the answer is for above, will be the
same for the compiler supplied copy constructor.

No.
 
M

mlimber

blangela said:
What does the default assignment operator (compiler supplied assignment
operator, sometimes also referred to as the implicit assignment
operator or the synthesized assignment operator) do when the class
contains a reference.

There is no implicitly genenerated assignment operator in this case,
just as there is not when there is a non-static const member (remember,
a reference is very much like a constant pointer).

[snip]
I realize this would be easy to confirm with some simple code, but I
don't have access to a C++ compiler at the moment.

You could see that attempts to use an implicitly generated assignment
operator wouldn't compile with Comeau's or Dinkumware's online tests.

Cheers! --M
 
B

blangela

So what happens in the second case I asked about (the implicit copy
ctor)?
blangela said:
What does the default assignment operator (compiler supplied assignment
operator, sometimes also referred to as the implicit assignment
operator or the synthesized assignment operator) do when the class
contains a reference.

There is no implicitly genenerated assignment operator in this case,
just as there is not when there is a non-static const member (remember,
a reference is very much like a constant pointer).

[snip]
I realize this would be easy to confirm with some simple code, but I
don't have access to a C++ compiler at the moment.

You could see that attempts to use an implicitly generated assignment
operator wouldn't compile with Comeau's or Dinkumware's online tests.

Cheers! --M
 
M

mlimber

blangela said:
So what happens in the second case I asked about (the implicit copy
ctor)?

Please don't top-post here
(http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.4).

A constant data member can only be initialized in the implicitly
generated copy constructor's initialization list. Given a class like
this:

class A
{
const int i_;
public:
A( int j ) : i_( j ) {}
};

the implicitly generated copy constructor would be equivalent to this:

A::A( const A& a )
: i_( a.i_ )
{}

Cheers! --M
 
F

Frederick Gotham

Alf P. Steinbach:
It's not generated in that case, because it can't be implemented.


If, you're hell-bent on making it work, then replace:

a = b;

with:

#include <new>

a.~T();
::new((void*)&a) T(b);
 
A

Alf P. Steinbach

* Frederick Gotham:
Alf P. Steinbach:



If, you're hell-bent on making it work, then replace:

a = b;

with:

#include <new>

a.~T();
::new((void*)&a) T(b);

.... without violating the semantics of C++ references, and breaking type
safety for classes derived from T... ;-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top