Re: Overloading return type. I'm lost.

Discussion in 'C++' started by Val, Jul 18, 2003.

  1. Val

    Val Guest

    Here is a slightly modiefied code. But nothing really changed. But it shows
    that "T operator=" still works for ((a=b)=c)=d;

    I have my suspicions regarding +=. But that is NOT the issue. Beware! :)

    #include "stdafx.h" //holds iostream

    class CSomeClass
    {
    private:
    int m_intInteger;
    public:
    CSomeClass operator=(const CSomeClass &i)
    { if(this!=&i)
    { this->m_intInteger = i.m_intInteger;
    }
    return *this;
    }

    void setInt(int i)
    {
    m_intInteger=i;
    }

    int getInt()
    {
    return m_intInteger;
    }
    };


    int main(int argc, char* argv[])
    {
    CSomeClass* a = new CSomeClass;
    CSomeClass* b = new CSomeClass;
    CSomeClass* c = new CSomeClass;
    CSomeClass* d = new CSomeClass;
    a->setInt(5);
    b->setInt(8);
    c->setInt(12);
    d->setInt(33);

    ((a=b)=c)=d; //works just fine

    cout<<"a: "<<a->getInt()<<endl;
    cout<<"b: "<<b->getInt()<<endl;
    cout<<"c: "<<c->getInt()<<endl;
    cout<<"d: "<<d->getInt()<<endl;

    return 0;
    }

    So here is the question:

    To understand what is going on we probably need to know what the compiler
    REALLY generates.

    Is there anybody who does know this? Or maybe someone has an explanation to
    offer without it... Pls do.





    "Val" <> wrote in message
    news:bf8nsr$ru0$...
    > Here is a bit of super simple code:
    >
    > #include "stdafx.h" //holds iostream
    >
    > class CSomeClass
    > {
    > private:
    > int m_intInteger;
    > public:
    > CSomeClass& operator=(const CSomeClass &i)
    > {
    > if(this!=&i)
    > {
    > this->m_intInteger = i.m_intInteger;
    > }
    > return *this;
    > }
    > void setInt(int i)
    > {
    > m_intInteger=i;
    > }
    >
    > int getInt()
    > {
    > return m_intInteger;
    > }
    > };
    >
    >
    > int main(int argc, char* argv[])
    > {
    > CSomeClass a,b;
    > a.setInt(5);
    > b.setInt(8);
    > b=a;
    >
    > cout<<a.getInt()<<endl;
    >
    > return 0;
    > }
    >
    > This piece of code puzzles me: CSomeClass& operator=(const CSomeClass &i)
    > I know everything about it except for one thing: Why CSomeClass& instead

    of
    > CSomeClass ?
    > Or in general why "T& operator=" instead of "T operator=".
    > Because both seem to work fantastic.
    > I can do: a=b; or b=a; all fine with both return types.
    >
    > So what is the ampersand return type doing for me in cases of operator
    > overloading.
    > Yes, I know what "T& ReturnFunc(T ) will do for me. But not in case of
    > operator overloading. I just can't figure it out...
    >
    > V~
    >
    >
     
    Val, Jul 18, 2003
    #1
    1. Advertising

  2. "Val" <> wrote in message
    news:bf8t1g$gh$...
    > Here is a slightly modiefied code. But nothing really changed. But it

    shows
    > that "T operator=" still works for ((a=b)=c)=d;
    >
    > I have my suspicions regarding +=. But that is NOT the issue. Beware! :)
    >
    > #include "stdafx.h" //holds iostream
    >
    > class CSomeClass
    > {
    > private:
    > int m_intInteger;
    > public:
    > CSomeClass operator=(const CSomeClass &i)
    > { if(this!=&i)
    > { this->m_intInteger = i.m_intInteger;
    > }
    > return *this;
    > }
    >
    > void setInt(int i)
    > {
    > m_intInteger=i;
    > }
    >
    > int getInt()
    > {
    > return m_intInteger;
    > }
    > };
    >
    >
    > int main(int argc, char* argv[])
    > {
    > CSomeClass* a = new CSomeClass;
    > CSomeClass* b = new CSomeClass;
    > CSomeClass* c = new CSomeClass;
    > CSomeClass* d = new CSomeClass;
    > a->setInt(5);
    > b->setInt(8);
    > c->setInt(12);
    > d->setInt(33);
    >
    > ((a=b)=c)=d; //works just fine
    >


    You are assigning POINTERS not objects! Try

    ((*a = *b) = *c) = *d;

    john
     
    John Harrison, Jul 18, 2003
    #2
    1. Advertising

  3. Val

    baojian Guest

    T& and not T, because we can use it this way, ((a=b)=c)=d
    your second example is not a good one.
    ((a=b)=c)=d do work fine only because they are just simple pointer
    assignment.
    they don't invoke the function CSomeClass operator=

    the correct example should like this:
    CSomeClass a;
    CSomeClass b;
    CSomeClass c;
    CSomeClass d;

    a.setInt(5);
    b.setInt(8);
    c.setInt(12);
    d.setInt(33);

    ((a=b)=c)=d; //works just fine

    cout<<"a: "<<a.getInt()<<endl;
    cout<<"b: "<<b.getInt()<<endl;
    cout<<"c: "<<c.getInt()<<endl;
    cout<<"d: "<<d.getInt()<<endl;

    if you use T&, then you'll get the answer you want, if you use T, then 8, 8,
    12, 33, because you can't assign c to an Object.



    "Val" <> дÈëÏûÏ¢ÐÂÎÅ
    :bf8t1g$gh$...
    > Here is a slightly modiefied code. But nothing really changed. But it

    shows
    > that "T operator=" still works for ((a=b)=c)=d;
    >
    > I have my suspicions regarding +=. But that is NOT the issue. Beware! :)
    >
    > #include "stdafx.h" //holds iostream
    >
    > class CSomeClass
    > {
    > private:
    > int m_intInteger;
    > public:
    > CSomeClass operator=(const CSomeClass &i)
    > { if(this!=&i)
    > { this->m_intInteger = i.m_intInteger;
    > }
    > return *this;
    > }
    >
    > void setInt(int i)
    > {
    > m_intInteger=i;
    > }
    >
    > int getInt()
    > {
    > return m_intInteger;
    > }
    > };
    >
    >
    > int main(int argc, char* argv[])
    > {
    > CSomeClass* a = new CSomeClass;
    > CSomeClass* b = new CSomeClass;
    > CSomeClass* c = new CSomeClass;
    > CSomeClass* d = new CSomeClass;
    > a->setInt(5);
    > b->setInt(8);
    > c->setInt(12);
    > d->setInt(33);
    >
    > ((a=b)=c)=d; //works just fine
    >
    > cout<<"a: "<<a->getInt()<<endl;
    > cout<<"b: "<<b->getInt()<<endl;
    > cout<<"c: "<<c->getInt()<<endl;
    > cout<<"d: "<<d->getInt()<<endl;
    >
    > return 0;
    > }
    >
    > So here is the question:
    >
    > To understand what is going on we probably need to know what the compiler
    > REALLY generates.
    >
    > Is there anybody who does know this? Or maybe someone has an explanation

    to
    > offer without it... Pls do.
    >
    >
    >
    >
    >
    > "Val" <> wrote in message
    > news:bf8nsr$ru0$...
    > > Here is a bit of super simple code:
    > >
    > > #include "stdafx.h" //holds iostream
    > >
    > > class CSomeClass
    > > {
    > > private:
    > > int m_intInteger;
    > > public:
    > > CSomeClass& operator=(const CSomeClass &i)
    > > {
    > > if(this!=&i)
    > > {
    > > this->m_intInteger = i.m_intInteger;
    > > }
    > > return *this;
    > > }
    > > void setInt(int i)
    > > {
    > > m_intInteger=i;
    > > }
    > >
    > > int getInt()
    > > {
    > > return m_intInteger;
    > > }
    > > };
    > >
    > >
    > > int main(int argc, char* argv[])
    > > {
    > > CSomeClass a,b;
    > > a.setInt(5);
    > > b.setInt(8);
    > > b=a;
    > >
    > > cout<<a.getInt()<<endl;
    > >
    > > return 0;
    > > }
    > >
    > > This piece of code puzzles me: CSomeClass& operator=(const CSomeClass

    &i)
    > > I know everything about it except for one thing: Why CSomeClass& instead

    > of
    > > CSomeClass ?
    > > Or in general why "T& operator=" instead of "T operator=".
    > > Because both seem to work fantastic.
    > > I can do: a=b; or b=a; all fine with both return types.
    > >
    > > So what is the ampersand return type doing for me in cases of operator
    > > overloading.
    > > Yes, I know what "T& ReturnFunc(T ) will do for me. But not in case of
    > > operator overloading. I just can't figure it out...
    > >
    > > V~
    > >
    > >

    >
    >
     
    baojian, Jul 20, 2003
    #3
    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. Josephine Schafer

    Re: Overloading return type. I'm lost.

    Josephine Schafer, Jul 18, 2003, in forum: C++
    Replies:
    1
    Views:
    312
    John Harrison
    Jul 18, 2003
  2. dropy
    Replies:
    2
    Views:
    334
    Jonathan Mcdougall
    Jul 28, 2003
  3. Ann Huxtable
    Replies:
    9
    Views:
    734
    benben
    Jul 17, 2005
  4. Madhav
    Replies:
    1
    Views:
    359
  5. Replies:
    0
    Views:
    310
Loading...

Share This Page