reference mebmber variable

Discussion in 'C++' started by siddhu, Dec 16, 2007.

  1. siddhu

    siddhu Guest

    If there is reference member variable in the class, why doesn't
    default assignment operator work?

    class A
    {
    int& i;
    public:
    A( int& ii):i(ii){}
    //A& operator=(const A& a){i = a.i;}
    };

    int main()
    {
    int k =10;
    A a(k);
    A b(k);
    a = b; //This does not compile
    }

    But if I uncomment my assignment operator it works. I hope default
    assignment operator works the same way.
    siddhu, Dec 16, 2007
    #1
    1. Advertising

  2. siddhu

    Ian Collins Guest

    siddhu wrote:
    > If there is reference member variable in the class, why doesn't
    > default assignment operator work?
    >
    > class A
    > {
    > int& i;
    > public:
    > A( int& ii):i(ii){}
    > //A& operator=(const A& a){i = a.i;}
    > };
    >
    > int main()
    > {
    > int k =10;
    > A a(k);
    > A b(k);
    > a = b; //This does not compile
    > }
    >
    > But if I uncomment my assignment operator it works. I hope default
    > assignment operator works the same way.


    Does your compiler give you a meaningful error message? It should!

    --
    Ian Collins.
    Ian Collins, Dec 16, 2007
    #2
    1. Advertising

  3. On Dec 16, 9:06 am, siddhu <> wrote:
    > If there is reference member variable in the class, why doesn't
    > default assignment operator work?
    >
    > class A
    > {
    > int& i;
    > public:
    > A( int& ii):i(ii){}
    > //A& operator=(const A& a){i = a.i;}
    >
    > };
    >
    > int main()
    > {
    > int k =10;
    > A a(k);
    > A b(k);
    > a = b; //This does not compile
    >
    > }
    >
    > But if I uncomment my assignment operator it works. I hope default
    > assignment operator works the same way.


    References are non-copyable/non-assignable. Your assignment operator
    works because you are not actually assigning one reference to another
    reference. The reference member 'i' in your case keeps referring to
    the same variable that it referred to when the object was constructed
    i.e. 'k'. And you are just assigning the value to it. As a result the
    value of 'k' will change upon assignment a=b;. You can verify using
    two different variables (having different initial values) to construct
    A objects a and b.

    I don't know what does the compiler generated assignment operator do,
    but it might just be giving the error seeing a (non-assignable)
    reference member. I am not sure how one would write a statement in C++
    which would do assignment of references because anything like this:

    reference_1 = reference_2;

    would not actually be assigning a reference to a reference but
    assigning variable referred to by reference_1 by value of variable
    referred to by reference_2.
    Abhishek Padmanabh, Dec 16, 2007
    #3
  4. On Sat, 15 Dec 2007 21:27:08 -0800, Abhishek Padmanabh wrote:

    > On Dec 16, 9:06 am, siddhu <> wrote:
    >> If there is reference member variable in the class, why doesn't default
    >> assignment operator work?
    >>
    >> class A
    >> {
    >> int& i;
    >> public:
    >> A( int& ii):i(ii){}
    >> //A& operator=(const A& a){i = a.i;}
    >>
    >> };
    >>
    >> int main()
    >> {
    >> int k =10;
    >> A a(k);
    >> A b(k);
    >> a = b; //This does not compile
    >>
    >> }
    >>
    >> But if I uncomment my assignment operator it works. I hope default
    >> assignment operator works the same way.

    [snip]
    > I don't know what does the compiler generated assignment operator do,
    > but it might just be giving the error seeing a (non-assignable)
    > reference member. I am not sure how one would write a statement in C++
    > which would do assignment of references because anything like this:
    >
    > reference_1 = reference_2;
    >
    > would not actually be assigning a reference to a reference but assigning
    > variable referred to by reference_1 by value of variable referred to by
    > reference_2.


    If an object contains a reference copy constructor and assignment won't
    be generated by compiler so asking what they do makes no sense - they
    simply don't exist. And references cannot be reseated.

    --
    Tadeusz B. Kopec ()
    Mr. Cole's Axiom:
    The sum of the intelligence on the planet is a constant;
    the population is growing.
    Tadeusz B. Kopec, Dec 16, 2007
    #4
  5. siddhu

    James Kanze Guest

    On Dec 16, 8:28 pm, "Tadeusz B. Kopec" <>
    wrote:
    > On Sat, 15 Dec 2007 21:27:08 -0800, Abhishek Padmanabh wrote:
    > > On Dec 16, 9:06 am, siddhu <> wrote:
    > >> If there is reference member variable in the class, why
    > >> doesn't default assignment operator work?


    > >> class A
    > >> {
    > >> int& i;
    > >> public:
    > >> A( int& ii):i(ii){}
    > >> //A& operator=(const A& a){i = a.i;}
    > >> };


    > >> int main()
    > >> {
    > >> int k =10;
    > >> A a(k);
    > >> A b(k);
    > >> a = b; //This does not compile
    > >> }


    > >> But if I uncomment my assignment operator it works. I hope
    > >> default assignment operator works the same way.

    > [snip]
    > > I don't know what does the compiler generated assignment
    > > operator do,


    It doesn't. If the class contains a reference, the compiler
    generates a declaration for the assignment operator (if the user
    doesn't provide one), but it is an error (requiring a
    diagnostic) if the compiler is required to generate the
    definition (because the operator was actually used).

    > > but it might just be giving the error seeing a
    > > (non-assignable) reference member. I am not sure how one
    > > would write a statement in C++ which would do assignment of
    > > references because anything like this:


    > > reference_1 = reference_2;


    > > would not actually be assigning a reference to a reference
    > > but assigning variable referred to by reference_1 by value
    > > of variable referred to by reference_2.


    > If an object contains a reference copy constructor and
    > assignment won't be generated by compiler so asking what they
    > do makes no sense - they simply don't exist. And references
    > cannot be reseated.


    There's no problem with the copy constructor, just assignment.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Dec 17, 2007
    #5
    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. Kench
    Replies:
    2
    Views:
    5,407
    Victor Bazarov
    Jun 28, 2004
  2. dost
    Replies:
    2
    Views:
    351
    Mark P
    Apr 25, 2006
  3. mfglinux
    Replies:
    11
    Views:
    695
    Roberto Bonvallet
    Sep 12, 2007
  4. Haofei Wang
    Replies:
    3
    Views:
    96
    Sy Ali
    Dec 30, 2006
  5. David Filmer
    Replies:
    19
    Views:
    237
    Kevin Collins
    May 21, 2004
Loading...

Share This Page