Thread: Why can = operator not be overloaded with member function?

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    225

    Why can = operator not be overloaded with member function?

    Hello,
    i want to know the reason as to why can't the = operator be overloaded with friend function of the class?why do we only have to use the member function??

    this is the code :-

    Code:
    #include <iostream.h>
    class set
    {
       int data;
       public:
    	   void getdata(int);
    	   void putdata();
    	   void operator=(set);
    };
    void set::getdata(int a)
    {
    	data=a;
    }
    void set::putdata()
    {
    	cout<<data;
    }
    void set::operator=(set b)
    {
    	data=b.data;
    }
    
    int main(void)
    {
     set a,b;
     a.getdata(5);
     a.putdata();
     b=a;
     cin.ignore();
     cin.get();
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    Please use friend function instead of this and tell me whether it can be overloaded or not?and if not then why?

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Simply put, because the standard says so. I don't know the actual reason, and Design and Evolution doesn't elaborate.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Could this have anything to do with the fact that the compiler would generate assignment operator automatically unless you define it yourself as a class method?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    It's as good a guess as any. Makes sense.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    A further suggestion to you:
    Use appropriate variable names (this may just be an example so I don't know if you do it in real code) and also use appropriate variable names in prototypes and class definitions.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It could be an oversight. See Non-member overloaded copy assignment operator.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    It was no oversight. The actual reason for the constraint is encapsulation.

    operator=() acts on it's left hand operand (i.e. "a = b" changes a directly; it does not create any temporaries, new objects, etc). operator=() is therefore part of the interface of the class. Allowing it to be a non-member function would allow other, unrelated, code to arbitrarily change how assignment works for the class. i.e. it would allow other code to directly change the class interface, and potentially introduce new behaviour that breaks the class, without knowledge of the class designer.

    constructors and destructors are required to be member functions for the same reason.

    It prevents things like this;
    Code:
    class BigInteger
    {
         // all sorts of things to make class work
    };
    
    // some evil code
    
    //  create an assignment operator with the body that effectively does this regardless of RHS
    
    BigInteger operator=(BigInteger &lhs, const BigInteger &rhs)
    {
        lhs.SetContents(42);   // set *this to be value of 42 regardless of value of rhs, and without knowledge of original class designer
    }
    Last edited by grumpy; 02-22-2008 at 03:00 PM.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Allowing it to be a non-member function would allow other, unrelated, code to arbitrarily change how assignment works for the class. i.e. it would allow other code to directly change the class interface, and potentially introduce new behaviour that breaks the class, without knowledge of the class designer.
    Section 3 of the proposal I linked to outlines possible solutions to this problem.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    I was not arguing there are no alternatives. I was explaining the reasoning behind it being as it currently is.

    The proposal you linked to would actually allow the sorts of problems I described in certain circumstances, not prevent them.

    There are always debates between people who want features for convenience and those who want to disallow such features because those features can be misapplied. This is one of those cases. Personally, I consider the proposal you linked to will introduce more problems than it solves.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Can you check what is wrong with this code
    By Ron in forum C++ Programming
    Replies: 4
    Last Post: 08-01-2008, 10:59 PM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM