1

I am working with a template class which parses data. Each line of data will require calling one of two functions to handle the data. This decision is determined at the time the parser is constructed and depends on variables passed to the constructor. I thought it would be useful to use a function pointer for this so that i could use one if statement in the constructor and assign the proper function to the function pointer which will be used in the body of the program. I am getting an error which I cannot figure out and I am curious if I am using the function pointer correctly in this context.

template<class T1, class T2>
class MyClass{
    protected:
        void (*pDoSomething)(std::string,std::string,std::string);
        void functionOne(std::string,std::string,std::string);
        void functionTwo(std::string,std::string,std::string);
    public:
        MyClass(bool option);
        void parseData();
};

templace<class T1, class T2>
MyClass<T1,T2,>::MyClass(bool option){
    if (option) pDoSomething = &functionOne;
    else pDoSomething = &functionTwo;
}

template<class T1, class T2>
void MyClass<T1,T2>::parseData(){
    /* . . . */
    while(dataToParse){
        *pDoSomething(string1, string2, string3);
    }
    /* . . . */
}
6
  • 4
    functionOne and functionTwo aren't functions. They are member functions. Totally different thing. Commented Apr 22, 2013 at 20:03
  • The easy fix is to make functionOne and functionTwo static, and thus turn them into functions. Commented Apr 22, 2013 at 21:19
  • @ Kerrek SB, thank you for the clarification. The answers below have made this clear. Commented Apr 23, 2013 at 18:35
  • @Yakk Said functions require the use of variables found within the object. Such functions cannot be made static within a class correct? Commented Apr 23, 2013 at 18:35
  • Yes, unless you pass a pointer to an instance into them, and use that pointer. static void functionOne(MyClass<T1,T2>* self, std::string,std::string,std::string); Commented Apr 23, 2013 at 18:59

2 Answers 2

3

Change it like so:

template<class T1, class T2>
class MyClass
{
    typedef void (MyClass::*ptmf)(std::string, std::string, std::string);

    ptmf the_function;

    explicit MyClass(bool b)
    : the_function(b ? &MyClass::functionOne : &MyClass::functionTwo)
    { }

    void parse_data()
    {
        (this->*the_function)(s1, s2, s3);
    }

    // ...
};
Sign up to request clarification or add additional context in comments.

1 Comment

+1 Just waiting on the "What does ->* do??" followup question now.
2

The error is most likely from the use of the function pointer to point to member functions. There is a considerable difference between functions that are in classes and just normal functions. You are assigning a class function to a normal function pointer. The difference comes in when you consider that all member functions take a hidden this pointer as their first argument.

Either change the functions to be outside of the class (or instead use static functions if the functions do not need any of the class's variables, bases or the this pointer) or change the function pointer to a class member function pointer. A pointer to a MyClass function that takes a string will look like this.

void (MyClass::*fptr)(std::string str);

Because the class function pointer requires a hidden this pointer the call of the function pointer changes also. To call the function pointed to by fptr you can use the ->* or .* c++ operators. So to call it using the this pointer of MyClass you can do this:

std::string aString;
(this->*fptr)(aString);

I'm not entirely sure but it maybe possible to do what you hope to do with virtual functions instead? This can be achieved by having two separate classes and a pointer to an instance of one of the two. Both classes derive from a class that has a pure virtual function that is the same as the function you are assigning here. This is a cleaner solution than using function pointers. I would look into this as you may find it useful if your looking for dynamic behavior using function pointers.

This is a good tutorial on the basics of function pointers:

http://www.learncpp.com/cpp-tutorial/78-function-pointers/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.