简体   繁体   中英

Using function pointers in template class c++

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);
    }
    /* . . . */
}

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);
    }

    // ...
};

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM