Function parameter overload without rewriting function



  • I have two functions that are the very same code with the only difference that they use different parameters.
    @
    int getFilterIndex(filter * pF);
    int getFilterIndex(ushort freq);
    ( ... )
    bool equalizer::removeFilter(filter * pF){
    int index = getFilterIndex(pF);
    if (index != -1){
    delete filterList.takeAt(index);

    return true;
    }

    return false;
    }

    bool equalizer::removeFilterAtFreq(ushort freq){
    int index = getFilterIndex(freq);
    if (index != -1){
    delete filterList.takeAt(index);

    return true;
    }

    return false;
    }@

    I was wondering if it was possible to write the same function once but to be possible to use the same parameter for either a pointer or an ushort ? As you can see the code of those two functions is just copy-paste, it would be a waste to use it like that. I have similar code for 10 other functions as well so your suggestions will be greatly appreciated!



  • You could use template functions.



  • I tried to but when i declare the template in my header file the cpp file would not recognize my type. I used this code:
    header
    @template<typename myType>

    class equalizer{
    ...
    void removeFilter(myType param);
    }@

    cpp
    @void equalizer::removeFilter(myType par){
    int index = getFilterIndex(freq);
    if (index != -1){
    delete filterList.takeAt(index);

    return true;
    }

    return false;
    }@

    myType in the cpp file is not recognized at all. Redeclaring the template in the cpp file doesn't change a thing.
    I never used templates before and I'm not sure what am I doing wrong...

    EDIT
    nevermind, I read a few tutorials and now I got it working. Thank you for help!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.