Thread: overloading the ^operator

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    4

    overloading the ^operator

    Hi,

    I have added the following function to my program:
    Code:
    void RobotTrajectory::operator^(double& RHS)
    {
    	if (set>2) RHS -= 2*RHS;
    }
    I want to be able to write:

    Code:
    Key = ^sin(alpha)*Kex+ ^cos(alpha)*Key;
    and if set is greater than 2, effectively make it
    Code:
    Key = -sin(alpha)*Kex+ (-cos(alpha)*Key);
    otherwise make it:
    Code:
    Key = sin(alpha)*Kex+ cos(alpha)*Key;
    The program runs fine as long as I don'y try to use my overloaded operator. If I do it throws the following error:
    error C2059: syntax error : '^'

    I have tried writting
    Code:
    Key = ^3.124
    and I get the same problem.

    Please could someone tell me what I am doing wrong!

    thanks,

    Leon

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    You are defining the operator^ as a member function. How do you call a member function on an object?

    In addition, what type of operator is operator^: unary or binary?
    Last edited by 7stud; 11-25-2005 at 06:05 AM.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    4
    >You are defining the operator^ as a member function. How do >you call a member function on an object?

    Sorry I forgot to mention - I am using .net, and I have called member functions before within my classes and it works just fine (I don't know why exactly) - I just tested it with another function and it compiled with no errors.


    >In addition, what type of operator is operator^: unary or binary?
    It is binary, but I tried it with a unary one (namely ~) and it gave me the following error:
    error C2808: unary 'operator ~' has too many formal parameters


    Leon

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Sorry I forgot to mention - I am using .net, and I have called member functions before within my classes and it works just fine
    Yes, but what is the syntax for calling a member function? It looks like this:

    myObj.someFunc();

    Does this look equivalent:

    ^sin(alpha)

    Where is the RobotTrajectory object on the left hand side of your operator? Pretend for a minute that you overloaded the binary operator+ for your RobotTrajectory object, which you defined to add an int to the RobotTrajectory object. To call your operator+ function on your RobotTrajectory object, you would have to do this:

    myRTObject + 2

    Notice, there is an object on the left, the operator in the middle, and the second entity on the right(binary). That is equivalent to:

    myRTObject.operator+(2);

    You have to call member functions with objects of the class. In this expression:

    ^sin(alpha)

    there is no object of the class. That's just you trying to convert the character '^' into a function call. That's not what operator overloading is about.
    Last edited by 7stud; 11-25-2005 at 07:16 AM.

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    4
    I see what you are saying, and that does actually work fine if called from a different class (thanks!), but I want to call it from another member function of the class it is a member of.

    The following code works fine:
    Code:
    void RobotTrajectory::sendCommand(void)
    {
      ....
    }
    
    void RobotTrajectory::ILCControl(double Kex, double Key)
    {
             sendCommand();
    }
    there is no object of the class. That's just you trying to convert the character '^' into a function call. That's not what operator overloading is about.
    I don't understand this distinction, why can't this happen?

    Thanks for you help so far!
    Last edited by LeonB; 11-25-2005 at 07:31 AM.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Why is this different from what I am trying to do with my overloaded operator
    Well, this is most likely not going to make any sense to you, but every member function has an invisible parameter. The invisible parameter is the 'this' pointer, which is something that refers to the object that called the function ILCControl(double Kex, double Key). So, inside that function when you write:

    sendCommand();

    The compiler translates that into:

    (*this).sendCommand()

    where (*this) is the object that called the function ILCControl(double Kex, double Key).

    To make an equivalent call to an operator^ function inside one of your member functions, you need to write something like:

    operator^(10.5);

    or

    (*this) ^ 10.5

    But, I don't know if .NET is the same as C++.
    Last edited by 7stud; 11-25-2005 at 07:37 AM.

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    4
    operator^(10.5);

    or

    (*this) ^ 10.5
    this works like a dream.

    I always wondered how 'this' worked, but it makes sense now.

    Thanks on both accounts!

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Please could someone tell me what I am doing wrong!
    Yes, unlike + and -, ^ does not have a unary form, so you can never have a unary use of the operator, even if you have coded an overload for it.

    Likewise, overloading ^ to mean pow() for example does not suddenly jump ^ way up the precedence table, which can lead to some counter-intuitive behaviour if you don't say what you mean with some parentheses.

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Yes, unlike + and -, ^ does not have a unary form, so you can never have a unary use of the operator, even if you have coded an overload for it.
    What wasn't apparent in the original post was that the op was actually trying to use the operator^ as a binary operator--by employing the implicit this pointer inside a member function. It's understandable why the op thought if you could do this:
    Code:
    void someClass:someFunc()
    {
             someOtherMemberFunc();
    }
    you should be able to do this:
    Code:
    void someClass:someFunc()
    {
            ^sin(45);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overloading operators
    By ugmusicbiz in forum C++ Programming
    Replies: 2
    Last Post: 02-13-2009, 01:41 PM
  2. unary operator overloading and classes
    By coletek in forum C++ Programming
    Replies: 9
    Last Post: 01-10-2009, 02:14 AM
  3. operator overloading for streams
    By rozner in forum C++ Programming
    Replies: 3
    Last Post: 10-23-2006, 08:00 PM
  4. overloading
    By theLukerBoy in forum C++ Programming
    Replies: 6
    Last Post: 11-04-2002, 08:49 PM
  5. passing discards qualifiers? overloading =...
    By Captain Penguin in forum C++ Programming
    Replies: 9
    Last Post: 10-07-2002, 05:38 PM