Thread: assignment operator overloading problem

  1. #1
    a newbie :p
    Join Date
    Aug 2008
    Zurich, Switzerland, Switzerland

    assignment operator overloading problem

    Dear all,

    Let's say I have this code:

    ClassA *a = foo();
    Here, I want to overload the assignment operator:

    ClassA *ClassA::operator = (ClassA *rhs)
    Is it correct?
    I had tried it but it failed. I tapped a breakpoint, but it didn't execute that part of code.


  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Hannover, Germany
    An assignment operator is supposed to assign something to an instance of your class. You are assigning something to a pointer to an instance of your class.

    ClassA a;

    a = something;

    ... would call an assignment operator. Your assignment operator as you coded it doesn't make much sense to me, can you explain what you want to achieve with it.

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    You want refferance not pointer. Also reccomended that you use const on the one you are assigning so as not to mess up data by accident. Also keep in mind if you want to assign from your foo() you better hope it returns an object of that class.

    class foo
       foo(int xx, int yy, int zz)
          x = xx;
          y = yy;
          z = zz;
       foo & operator=(const foo &rhs)
          x = rhs.x;
          y = rhs.y;
          z = rhs.z;
       int x,y,z;
    Last edited by ~Kyo~; 05-18-2011 at 09:53 AM.

  4. #4
    Join Date
    Oct 2007
    Inside my computer
    Don't forget to check for self-assignment. It can mess up things badly. I know I tend to forget.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overloading Assignment Operator, this pointer
    By cuo741 in forum C++ Programming
    Replies: 11
    Last Post: 12-09-2010, 09:12 AM
  2. Copy Constructors and Assignment Operator overloading.
    By leeor_net in forum C++ Programming
    Replies: 1
    Last Post: 11-09-2009, 10:26 PM
  3. Assignment operator overloading
    By hsgw in forum C++ Programming
    Replies: 1
    Last Post: 01-20-2007, 06:44 PM
  4. Replies: 9
    Last Post: 05-19-2006, 05:19 PM
  5. overloading the assignment operator
    By Unregistered36 in forum C++ Programming
    Replies: 1
    Last Post: 11-30-2001, 06:51 AM

Tags for this Thread