Compiler supplied assignment operator and references

Discussion in 'C++' started by blangela, Nov 7, 2006.

  1. blangela

    blangela Guest

    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.
    blangela, Nov 7, 2006
    #1
    1. Advertising

  2. * 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?
    Alf P. Steinbach, Nov 7, 2006
    #2
    1. Advertising

  3. blangela

    mlimber Guest

    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
    mlimber, Nov 7, 2006
    #3
  4. blangela

    blangela Guest

    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
    blangela, Nov 7, 2006
    #4
  5. blangela

    mlimber Guest

    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-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
    mlimber, Nov 7, 2006
    #5
  6. blangela

    blangela Guest

    Thanks M! Much clearer now!

    Bob L.
    blangela, Nov 7, 2006
    #6
  7. 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
    Frederick Gotham, Nov 7, 2006
    #7
  8. * 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?
    Alf P. Steinbach, Nov 7, 2006
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mg

    Evaluates a supplied string

    mg, Dec 23, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    383
  2. G Patel

    comma operator and assignment operator

    G Patel, Feb 7, 2005, in forum: C Programming
    Replies:
    4
    Views:
    478
    Barry Schwarz
    Feb 8, 2005
  3. Chris
    Replies:
    34
    Views:
    1,483
  4. Hicham Mouline
    Replies:
    2
    Views:
    679
    Juha Nieminen
    Sep 1, 2009
  5. Peng Yu
    Replies:
    3
    Views:
    384
Loading...

Share This Page