Thread: multiple operators in functors

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    204

    multiple operators in functors

    Hi
    I understand using functors and the operator however I was wondering if you can have more than one method in a class that can be called as though its a function?
    so if I had
    Code:
    class add_x {
      add_x(int x) : x(x) {} //set member variable to value passed in
      int operator()(int y) { return x + y; }
    
    private:
      int x;
    };
    
    then in main:
    
    add_x adder(2); // create an instance of the functor class
    int i = adder(8); // and "call" it
    serial.printf("%d\n", i);
    what if there was another method on add_x class called double
    could I have another operator in some way that called that method double so whatever number was passed to it it doubled..?

    Do I need templates for this or something?

    Thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by a.mlw.walker
    what if there was another method on add_x class called double
    could I have another operator in some way that called that method double so whatever number was passed to it it doubled..?
    Well, you certainly cannot use the name double since it is already the name of a built-in type. But now consider: your class is named add_x. Why add not create another class, maybe double_x, for this purpose?
    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
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    Your questions don't really make sense.

    1): Methods are functions. You are never calling a method "as though its a function" because they are functions.

    2): You can have as many overloads of the call operator as you wish. Methods are really just flavorful functions; you can overload them as normal. As with function overloading, you can't have two methods with the same signature visible in the same scope performing a different operation.

    3): You can have a method named almost anything you like. You can basically have any method call any other method. The call operator can call any other method you like as it is, as before, basically just a function; it only has a special name.

    4): Why would a class named `add_x' ever perform an operation called "double"? Why not just have a class named "double"? (You can't call it "double", but that's not the point.)

    5): You don't need templates. You can use templates. I'd even recommend that you use templates so that the types operated on are appropriate to the purpose.

    Soma

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    204
    I just made "double" up as I was typing to try and give an example of another thing I might want that class to be able to do. Sorry for confusion, I understand I could just make another class to do the job, but Im thinking of a situation where you may need to be able to treat an object as a function but also be able to do more than just one thing with it?
    I can overload the operator as long as Im not passing it the same types right?

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, the normal overloading rules apply.
    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. Replies: 4
    Last Post: 12-04-2011, 10:49 PM
  2. List of Functors
    By golfinguy4 in forum C++ Programming
    Replies: 13
    Last Post: 09-29-2009, 01:53 PM
  3. for_each and functors
    By KIBO in forum C++ Programming
    Replies: 2
    Last Post: 08-10-2009, 07:00 AM
  4. Storing functors?
    By Elysia in forum C++ Programming
    Replies: 18
    Last Post: 11-13-2008, 03:21 AM
  5. Functors
    By cboard_member in forum C++ Programming
    Replies: 9
    Last Post: 04-25-2006, 09:47 AM