Thread: Overloaded Operators, and Pointers

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    23

    Overloaded Operators, and Pointers

    I have made a simple class which overloads an equals operator. So far, it does nothing important, becuase its just a test.

    The class just looks like this:
    Code:
    class myoverloadAttempt
    {
    public:
    	int& val;
    
    	myoverloadAttempt(int& x) : val(x) {};
    
    	myoverloadAttempt& operator=(const int& x)
    	{ 
    		val=x;  
    		return *this;
    	};
    
    };
    and here is how I use it:
    Code:
    int myvar = 2;
    myoverloadAttempt o(myvar);
    o = 5;
        //myoverloadAttempt* o = new myoverloadAttempt(myvar);
        //o->operator =(5);
    std::cout<<myvar<<"\n";
    It works fine with those 2 lines commented out, ie using the class not as a pointer, but when I switch over and try to use it where o is a pointer to this class, o=5 no longer works. It says "error C2440: '=' : cannot convert from 'int' to 'myoverloadAttempt *'"
    When o is a pointer, It only worked when I used the line, o->operator=(5)
    .

    Is this always the case? Is there no way to use overloaded operators when dealing with a pointer?

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Is this always the case? Is there no way to use overloaded operators when dealing with a pointer?
    Just write it this way instead:

    Code:
    *o = 5;
    Also, holding a reference to an outside object (like int& x) is generally a bad idea, since the object has no control over the external object's lifetime. I understand that this is just a little example, but still good to remember.

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    You need to comment out your other "o" when using your other "o"
    Code:
    int myvar = 2;
        //myoverloadAttempt o(myvar);
        //o = 5;
    myoverloadAttempt* o = new myoverloadAttempt(myvar);
    o->operator =(5);
    std::cout<<myvar<<"\n";
    gg

  4. #4
    Registered User
    Join Date
    Dec 2005
    Posts
    23
    codeplug, I know, that was the only way it worked, but I wanted to just use o=5.

    And brewbuck, that works, so I will use that. Thanks, its not 100&#37; what I wanted, but what I wanted obviously doesn't exist, haha, so *o it is.

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Codeplug View Post
    You need to comment out your other "o" when using your other "o"
    I think he was saying that that DOES work, but wanted to know how to avoid that ugly use of the "operator" keyword. The answer is to use '*' to dereference first.

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by esaptonor View Post
    And brewbuck, that works, so I will use that. Thanks, its not 100% what I wanted, but what I wanted obviously doesn't exist, haha, so *o it is.
    Nah, you can do what you want, I just don't see the point:

    Code:
    myObject *o = new myObject();
    myObject &oref = *o;
    oref = 5;
    Basically you turn the pointer into a reference, eliminating the need to use '*'. But really, I don't see the point. The star is not that big of a deal.

  7. #7
    Registered User
    Join Date
    Dec 2005
    Posts
    23
    Ok, your right in that turning it into a reference is bad, I was hoping for some kind of modification to the operator overload part of the class...I assumed I was doing it wrong. But nah, I will stick with the * compared to having to turn it into a reference.

    Thanks a lot

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by esaptonor View Post
    And brewbuck, that works, so I will use that. Thanks, its not 100% what I wanted, but what I wanted obviously doesn't exist, haha, so *o it is.
    Doing just o = 5 tries to assign 5 to the pointer and not the object. A pointer needs to be dereferenced before it's accessed, so *o = 5 is the right way to go.
    You can also use const int& x, which is better since operator = doesn't change x.

  9. #9
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> o=5 no longer works

    Oh *that* part doesn't work

    You could do some crazy stuff *if* the language allowed for:
    Code:
    void operator=(myoverloadAttempt *p, int i) {*p = i;}
    Which it doesn't

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Bolean Operators hurt my head. (Trouble understanding) :(
    By Funcoot in forum C++ Programming
    Replies: 3
    Last Post: 01-20-2008, 07:42 PM
  3. link lists, pointers, using input\output file
    By chood72 in forum C++ Programming
    Replies: 1
    Last Post: 10-30-2005, 05:21 PM
  4. dangerous pointers
    By caroundw5h in forum C Programming
    Replies: 17
    Last Post: 07-06-2004, 09:49 PM
  5. overloaded on overloads
    By emceedee in forum C++ Programming
    Replies: 1
    Last Post: 03-31-2003, 02:14 AM