My overlaoded assignment operator doesn't work

Discussion in 'C++' started by heng, Dec 4, 2006.

  1. heng

    heng Guest

    I define my own assignment operator, but it doesn't work as I imagined.

    #include<iostream>
    #include <string>
    using namespace std;

    class A{
    int x;
    public:
    int y;
    int z;
    A& operator=(const A& t)
    {
    z=t.y;
    y=t.z;
    return *this;
    }
    };

    int main()
    {
    A aclass;
    aclass.y=9;
    aclass.z=11;
    A bclass=aclass;
    cout<<aclass.z<<endl<<bclass.y;
    string str;
    cin>>str;
    return 0;
    }
     
    heng, Dec 4, 2006
    #1
    1. Advertising

  2. heng

    Rolf Magnus Guest

    heng wrote:

    > I define my own assignment operator, but it doesn't work as I imagined.


    That's unfortunate. What did you imagine, and what does it do instead?

    > #include<iostream>
    > #include <string>
    > using namespace std;
    >
    > class A{
    > int x;
    > public:
    > int y;
    > int z;
    > A& operator=(const A& t)
    > {
    > z=t.y;
    > y=t.z;
    > return *this;
    > }
    > };
    >
    > int main()
    > {
    > A aclass;
    > aclass.y=9;
    > aclass.z=11;
    > A bclass=aclass;
    > cout<<aclass.z<<endl<<bclass.y;
    > string str;
    > cin>>str;
    > return 0;
    > }


    This program never calls operator=. Note that

    > A bclass=aclass;


    is not assigment, but initialization.
     
    Rolf Magnus, Dec 4, 2006
    #2
    1. Advertising

  3. heng

    heng Guest

    Rolf Magnus wrote:
    > This program never calls operator=. Note that
    >
    > > A bclass=aclass;

    >
    > is not assigment, but initialization.


    Thanks. How may I call operator=? Could you please show me an example?

    And I am kind of confused about assignment and initialization. Is the
    following belonging to assignment?

    A bclass;
    bclass=aclass;
     
    heng, Dec 4, 2006
    #3
  4. heng

    Rolf Magnus Guest

    heng wrote:

    > Rolf Magnus wrote:
    >> This program never calls operator=. Note that
    >>
    >> > A bclass=aclass;

    >>
    >> is not assigment, but initialization.

    >
    > Thanks. How may I call operator=? Could you please show me an example?
    >
    > And I am kind of confused about assignment and initialization.


    It's rather simple:

    Type Object = OtherObject; <- initialization
    Object = OtherObject; <- assignment

    In other words: if you define a new object, it's initialization, if you have
    an already existing object on the left side, it's assignment.

    > Is the following belonging to assignment?
    >
    > A bclass;
    > bclass=aclass;


    Yes.
     
    Rolf Magnus, Dec 4, 2006
    #4
  5. heng

    Kai-Uwe Bux Guest

    heng wrote:

    > I define my own assignment operator, but it doesn't work as I imagined.
    >
    > #include<iostream>
    > #include <string>
    > using namespace std;
    >
    > class A{
    > int x;
    > public:
    > int y;
    > int z;
    > A& operator=(const A& t)
    > {
    > z=t.y;
    > y=t.z;


    I would expect:

    z = t.z;
    y = t.y;

    Are you sure you want that?

    > return *this;
    > }
    > };
    >
    > int main()
    > {
    > A aclass;
    > aclass.y=9;
    > aclass.z=11;
    > A bclass=aclass;


    This calls the copy constructor.

    > cout<<aclass.z<<endl<<bclass.y;
    > string str;
    > cin>>str;
    > return 0;
    > }



    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Dec 4, 2006
    #5
  6. heng

    heng Guest

    Thanks.

    Kai-Uwe Bux wrote:
    > heng wrote:
    >
    > > I define my own assignment operator, but it doesn't work as I imagined.
    > >
    > > #include<iostream>
    > > #include <string>
    > > using namespace std;
    > >
    > > class A{
    > > int x;
    > > public:
    > > int y;
    > > int z;
    > > A& operator=(const A& t)
    > > {
    > > z=t.y;
    > > y=t.z;

    >
    > I would expect:
    >
    > z = t.z;
    > y = t.y;
    >
    > Are you sure you want that?
    >
    > > return *this;
    > > }
    > > };
    > >
    > > int main()
    > > {
    > > A aclass;
    > > aclass.y=9;
    > > aclass.z=11;
    > > A bclass=aclass;

    >
    > This calls the copy constructor.
    >
    > > cout<<aclass.z<<endl<<bclass.y;
    > > string str;
    > > cin>>str;
    > > return 0;
    > > }

    >
    >
    > Best
    >
    > Kai-Uwe Bux
     
    heng, Dec 4, 2006
    #6
  7. heng wrote:
    > I define my own assignment operator, but it doesn't work as I imagined.
    >
    > #include<iostream>
    > #include <string>
    > using namespace std;
    >
    > class A{
    > int x;
    > public:
    > int y;
    > int z;
    > A& operator=(const A& t)
    > {
    > z=t.y;
    > y=t.z;
    > return *this;
    > }
    > };
    >
    > int main()
    > {
    > A aclass;
    > aclass.y=9;
    > aclass.z=11;
    > A bclass=aclass;
    > cout<<aclass.z<<endl<<bclass.y;
    > string str;
    > cin>>str;
    > return 0;
    > }


    This invokes the copy constructor, not the assignment
    operator. Why not define that one too?

    HTH,
    - J.
     
    Jacek Dziedzic, Dec 4, 2006
    #7
  8. heng

    TvN Guest

    Hi,
    > I define my own assignment operator, but it doesn't work as I imagined.

    [skiped]

    People already said why it was not called, my reply about your
    operator;) Why do you check for self assigment? What about such
    situation:

    A a;
    ....
    a = a;

    So I think you should always check for this:

    A& operator=(const A& t)
    {
    if (this != &t)
    {
    ....
    }
    return *this;
    }

    Regards,
    Volodymyr!
     
    TvN, Dec 5, 2006
    #8
  9. heng

    red floyd Guest

    TvN wrote:
    > Hi,
    >> I define my own assignment operator, but it doesn't work as I imagined.

    > [skiped]
    >
    > People already said why it was not called, my reply about your
    > operator;) Why do you check for self assigment? What about such
    > situation:
    >
    > A a;
    > ...
    > a = a;
    >
    > So I think you should always check for this:
    >
    > A& operator=(const A& t)
    > {
    > if (this != &t)
    > {
    > ....
    > }
    > return *this;
    > }
    >


    See Sutter's "Exceptional C++" for a discussion of self-assignment
    checks. It should only be necessary as an optimization, if you use the
    construct-and-swap metaphor.
     
    red floyd, Dec 5, 2006
    #9
    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. G Patel

    comma operator and assignment operator

    G Patel, Feb 7, 2005, in forum: C Programming
    Replies:
    4
    Views:
    507
    Barry Schwarz
    Feb 8, 2005
  2. Replies:
    4
    Views:
    384
    Victor Bazarov
    Nov 9, 2005
  3. Chris
    Replies:
    34
    Views:
    1,594
  4. Hicham Mouline
    Replies:
    2
    Views:
    734
    Juha Nieminen
    Sep 1, 2009
  5. Replies:
    9
    Views:
    123
    matty
    Nov 20, 2005
Loading...

Share This Page