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

This is a discussion on Is it possible(another one!) to 'define' an operator() from a function pointer? within the C++ Programming forums, part of the General Programming Boards category; I was wondering if the above was possible since it would reduce some layers of redirection in my code. Code: ...

  1. #1
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498

    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);
    }
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,704
    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, 07: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, 10:05 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21