Thread: Is it possible(another one!) to 'define' an operator() from a function pointer?

  1. #1
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657

    Is it possible(another one!) to 'define' an operator() from a function pointer?

    I was wondering if the above was possible since it would reduce some layers of redirection in my code.
    Code:
    class Operator : public token
    {
    public:
        //Operator(std::string input);
        Operator(std::string input,int p,Assoc a);
        Operator(){};//DUMMY..remove later
        int prec;
        Assoc assoc;
        void operator()(std::vector<token*>& instack);
    };
    Constructor:
    Code:
    Operator::Operator(std::string input,int p,Assoc a)
    {
        this->type=op;
        this->raw=input;
        this->prec = p;
        this->assoc = a;
    
        &operator() = opmap[input]; 
        //This is erroneous ..but illustrates what I'd like to do.(Note that there isn't any mismatch
     //in type of the functions. Also, opmap is map mapping strings to function pointers.)
    
    }
    What I have now for the operator()
    Code:
    void Operator::operator()(std::vector<token*>& instack)
    {
        (opmap[this->raw])(instack);
    }

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Well a function pointer uses the operator () to execute its own code. You're not going to get away from that. I mean you could look at function pointers as a type that defines operator () and you aren't allowed to overload it for the same reason you can't overload operators on other built-in types.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to define function pointer
    By leetow2003 in forum C Programming
    Replies: 6
    Last Post: 08-19-2011, 08:08 AM
  2. define operator<< for std::pair<string, int>
    By patiobarbecue in forum C++ Programming
    Replies: 2
    Last Post: 02-07-2009, 08:29 AM
  3. How can I define and pass a pointer to a vector object?
    By asmileguo in forum C++ Programming
    Replies: 3
    Last Post: 09-07-2006, 11:19 AM
  4. How do i define this function?
    By misplaced in forum C++ Programming
    Replies: 1
    Last Post: 04-06-2005, 01:38 AM
  5. How to define operator = in CStringArray?
    By ooosawaddee3 in forum C++ Programming
    Replies: 4
    Last Post: 11-02-2002, 11:05 AM