11 Replies - 1587 Views - Last Post: 19 November 2010 - 06:48 AM Rate Topic: ***-- 2 Votes

#1 bnc  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 108
  • Joined: 18-March 10

assignment operator

Posted 19 November 2010 - 03:47 AM

Why are we using

return *this;

in assignment operators;

//ComplexN& ComplexN::operator=(const ComplexN& C)
void ComplexN::operator=(const ComplexN& C)
{
        if( this != &C)
        {
                a = C.a;
                b = C.b;
        }
        //return *this;
}



Y can't we write assignment operator like that? is any problem?

Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: assignment operator

#2 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 991
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: assignment operator

Posted 19 November 2010 - 04:12 AM

Try it.
Does it work?

There is your answer.
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: assignment operator

Posted 19 November 2010 - 04:13 AM

This smells like a test/quiz question. How can you return void from an assignment operator? If the assignment didn't return anything, how would you use it? Using an assignment operator:
MyObject obj2 = obj1;

If it returned void, you could not have an expression like that, could you?
Was This Post Helpful? 0
  • +
  • -

#4 bnc  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 108
  • Joined: 18-March 10

Re: assignment operator

Posted 19 November 2010 - 04:17 AM

janotte it is working...
#include <iostream>
#include <string.h>
#include <stdlib.h>

class ComplexN
{
private:
        char *string;
public:
        //ComplexN& operator=(const ComplexN& );
        void operator=(const ComplexN& );
        ComplexN(const ComplexN& );

        ComplexN();
        ~ComplexN();
        void setString(char *s)
        {
                int len = strlen(s);
                string = (char*) malloc((sizeof(char) * len) + 1);
                strcpy(string,s);
        }
        void showString()
        {
                std::cout << "string is " << string << std::endl;
        }
};

ComplexN::ComplexN()
{
        string = NULL;
}

ComplexN::ComplexN(const ComplexN& C)
{
}

//ComplexN& ComplexN::operator=(const ComplexN& C)
void ComplexN::operator=(const ComplexN& C)
{
        if( this != &C)
        {
                std::cout << "came here " << std::endl;
                if(string != NULL)
                        free(string);
                int len = strlen(C.string);
                string = (char*) malloc((sizeof(char) * len) + 1);
                strcpy(string,C.string);
        }
        //return *this;
}

ComplexN::~ComplexN()
{
        free(string);
}

int main()
{
        ComplexN C1;
        C1.setString("narendra");
        C1.showString();
        ComplexN C2;
        C2.setString("nani");
        C2.showString();
        std::cout << "after changing" << std::endl;
        C2 = C1;
        C2.showString();
}



output is
string is narendra
string is nani
after changing
came here
string is narendra

This post has been edited by bnc: 19 November 2010 - 04:18 AM

Was This Post Helpful? 0
  • +
  • -

#5 bnc  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 108
  • Joined: 18-March 10

Re: assignment operator

Posted 19 November 2010 - 04:40 AM

The above code is working perfectly....

anybody explain please...how come an assignment operator "returning void" is working?
Was This Post Helpful? 0
  • +
  • -

#6 bnc  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 108
  • Joined: 18-March 10

Re: assignment operator

Posted 19 November 2010 - 05:14 AM

Or give an example when will it will prodice the wrong output(seg fault, data corruption, what ever...) if I use void return type for an assignment operator like I did it in the previous posts.
Was This Post Helpful? 0
  • +
  • -

#7 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: assignment operator

Posted 19 November 2010 - 05:15 AM

Interesting...it does "work". I suspect suspect that there may be cases where the assignment operator may be called where it might not though. You could be treading a fine line.

The question is, WHY do you want to change the method signature of the assignment operator to NOT return a reference to the object?
Was This Post Helpful? 0
  • +
  • -

#8 bnc  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 108
  • Joined: 18-March 10

Re: assignment operator

Posted 19 November 2010 - 05:18 AM

inside the assignement operator we are already working with "this operator only"
string is equivalent to this->string or *this.string ....

so we are changing the contents of "this" only....

Then why should we again return *this..just for the sake of return type..(C2 = C1).....

then when we are using void as return type ..it should not compile or it should stop in the middle.....saying the return types did not match... I just want to know why is it working after changing the signature :)

This post has been edited by bnc: 19 November 2010 - 05:30 AM

Was This Post Helpful? 0
  • +
  • -

#9 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: assignment operator

Posted 19 November 2010 - 05:35 AM

Here we go, just as I suspected. It's amazing what a little searching can do, you know?

Quote

Return *this. This is necessary to allow multiple assignment, eg x = y = z;

Was This Post Helpful? 0
  • +
  • -

#10 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 991
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: assignment operator

Posted 19 November 2010 - 05:48 AM

Can I also suggest that, since you are working in C++ you dump all the nasty C-string stuff.

Use C++ strings and make your life easier and your code cleaner.

Have a look at these snippets and contrast them with your equivalents

private:
        std::string theString;

// ...

        void setString(std::string inString)
        {
			theString = inString;
        }




Neater and easier to write and read don't you think?
Was This Post Helpful? 0
  • +
  • -

#11 bnc  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 108
  • Joined: 18-March 10

Re: assignment operator

Posted 19 November 2010 - 05:51 AM

Thanks jack... thank you so much.... You have shown me an condition where it is failing if you return void....

I have tried .. it is giving error...

class6.cpp:72: error: no match for ‚operator=‚ in ‚C3 = C2.ComplexN::operator=(((const ComplexN&)((const ComplexN*)(& C1))))‚
class6.cpp:39: note: candidates are: void ComplexN::operator=(const ComplexN&)

but can you expalin me a little what is going wrong when we give x = y = z
and when we give x = y

Thank you

thanks janotte... will do that....
Was This Post Helpful? 0
  • +
  • -

#12 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 991
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: assignment operator

Posted 19 November 2010 - 06:48 AM

This is worth a read on this subject
http://icu-project.o...t_operator.html
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1