Thread: C++ Operator overloading

  1. #1
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528

    C++ Operator overloading

    These are probably silly questions

    Is is possible to force derived classed to overload certain operators, like we do with pure virtual functions?

    Is this possible to dynamically bind objects to their respective overloaded operators?

    I am getting errors with undefined references to my base class vtable when I hackly try to overload:

    Code:
    operator+
    I am not sure whether this is possible.
    Last edited by Aslaville; 12-15-2014 at 01:25 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Implement the operator with respect to the base class using a pure virtual member function which will derived classes will then override. You can make this pure virtual member function private so that you only expose the operator as part of the interface rather than having two things in the same interface to do the same thing.

    This approach is sometimes used to overload operator<< for standard output streams in a base class, with a (pure) virtual print member function that derived classes override.
    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

  3. #3
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    Quote Originally Posted by laserlight View Post
    Implement the operator with respect to the base class using a pure virtual member function which will derived classes will then override. You can make this pure virtual member function private so that you only expose the operator as part of the interface rather than having two things in the same interface to do the same thing.

    This approach is sometimes used to overload operator<< for standard output streams in a base class, with a (pure) virtual print member function that derived classes override.
    Well, Thanks. It seems I was making a silly mistake somewhere.

    Is this possible to dynamically bind objects to their respective overloaded operators?
    This seems like a very silly question because dynamic binding is based on pointers while in operators work the other way round.
    Last edited by Aslaville; 12-15-2014 at 07:55 PM.

  4. #4
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    Quote Originally Posted by laserlight View Post
    Implement the operator with respect to the base class using a pure virtual member function which will derived classes will then override. You can make this pure virtual member function private so that you only expose the operator as part of the interface rather than having two things in the same interface to do the same thing.

    This approach is sometimes used to overload operator<< for standard output streams in a base class, with a (pure) virtual print member function that derived classes override.
    This could have a problem though because as one cannot instantiate a class with pure virtual functions one can also not return an instance of a class with pure virtual functions.

    For instance if you want to overload operator +, you should have something like:

    Code:
    virtual baseclass operator + (const baseclass &);
    In this case, you will have a problem with the return type.

    I solved my problem more hackly by tracking my types, so there is nothing to worry about.

    I also came across this c++ - overriding pure virtual operators - Stack Overflow

  5. #5
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    This seems like a very silly question because dynamic binding is based on pointers while in operators work the other way round.
    O_o

    I've looked at this for a few minutes, and I can't figure out what you are actually saying.

    I solved my problem more hackly by tracking my types, so there is nothing to worry about.
    I'm also not sure about what this comment is saying.

    If you are "tacking your types" just to overload an operator, you are almost certainly doing something very unnecessary for very little reason.

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  6. #6
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    Quote Originally Posted by phantomotap View Post

    O_o

    I've looked at this for a few minutes, and I can't figure out what you are actually saying.
    Sorry, that was not very clear, worse I sensed it was not and I probably have no point

    Polymorphism only works on non-value types; references and pointers while operators work on value types(Am not sure that is correct)

    Quote Originally Posted by phantomotap View Post

    I'm also not sure about what this comment is saying.

    If you are "tacking your types" just to overload an operator, you are almost certainly doing something very unnecessary for very little reason.

    Soma
    Yeah, Its probably not that necessary but if it could work, it would probably save me a lot of bookkeeping

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Operators can accept their arguments by reference (if they change the argument) or by const reference (if they are not). Operators that are members of the class receive the "this" pointer, from which a reference to the corresponding argument can be obtained.

    Since they receive references, they can exploit virtual function dispatch. Which means, according to your inaccurate description, "polymorphism works".
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  8. #8
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    Quote Originally Posted by grumpy View Post
    Operators can accept their arguments by reference (if they change the argument) or by const reference (if they are not). Operators that are members of the class receive the "this" pointer, from which a reference to the corresponding argument can be obtained.

    Since they receive references, they can exploit virtual function dispatch. Which means, according to your inaccurate description, "polymorphism works".
    Yeah, I think my wording is inaccurate but I don't know how else to put it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. operator overloading
    By Clayg in forum C++ Programming
    Replies: 14
    Last Post: 01-13-2012, 01:22 PM
  2. Operator Overloading
    By BKurosawa in forum C++ Programming
    Replies: 22
    Last Post: 07-25-2007, 12:02 AM
  3. overloading operator <<
    By noob2c in forum C++ Programming
    Replies: 4
    Last Post: 09-23-2003, 10:11 AM
  4. operator overloading
    By tsut in forum C++ Programming
    Replies: 1
    Last Post: 03-29-2002, 09:26 AM
  5. operator overloading
    By Kohatian 3279 in forum C++ Programming
    Replies: 1
    Last Post: 03-28-2002, 09:00 AM