Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Compiler supplied assignment operator and references

Reply
Thread Tools

Compiler supplied assignment operator and references

 
 
blangela
Guest
Posts: n/a
 
      11-07-2006
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.

 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      11-07-2006
* 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?
 
Reply With Quote
 
 
 
 
mlimber
Guest
Posts: n/a
 
      11-07-2006
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

 
Reply With Quote
 
blangela
Guest
Posts: n/a
 
      11-07-2006
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


 
Reply With Quote
 
mlimber
Guest
Posts: n/a
 
      11-07-2006
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

 
Reply With Quote
 
blangela
Guest
Posts: n/a
 
      11-07-2006
Thanks M! Much clearer now!

Bob L.

 
Reply With Quote
 
Frederick Gotham
Guest
Posts: n/a
 
      11-07-2006
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
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      11-07-2006
* 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?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
POD and assignment operator and test operator Hicham Mouline C++ 2 09-01-2009 06:00 PM
conditions for automatic generation of default ctor, copy ctor,and default assignment operator (operator) puzzlecracker C++ 8 04-15-2008 09:56 PM
Assignment operator self-assignment check Chris C++ 34 09-26-2006 04:26 AM
pysqlite2.dbapi2.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 0, and there are -1 supplied. F. GEIGER Python 3 05-18-2005 03:46 PM
comma operator and assignment operator G Patel C Programming 4 02-08-2005 02:53 AM



Advertisments