By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,795 Members | 1,832 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,795 IT Pros & Developers. It's quick & easy.

Compiler supplied assignment operator and references

P: n/a
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.

Nov 7 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
* 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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 7 '06 #2

P: n/a
blangela wrote:
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

Nov 7 '06 #3

P: n/a
So what happens in the second case I asked about (the implicit copy
ctor)?

mlimber wrote:
blangela wrote:
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
Nov 7 '06 #4

P: n/a
blangela wrote:
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-lit....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

Nov 7 '06 #5

P: n/a
Thanks M! Much clearer now!

Bob L.

Nov 7 '06 #6

P: n/a
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);

--

Frederick Gotham
Nov 7 '06 #7

P: n/a
* 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);
.... without violating the semantics of C++ references, and breaking type
safety for classes derived from T... ;-)

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 7 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.