Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Function parameter overload without rewriting function
Qt 6.11 is out! See what's new in the release blog

Function parameter overload without rewriting function

Scheduled Pinned Locked Moved C++ Gurus
3 Posts 2 Posters 1.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T3STYT Offline
    T3STYT Offline
    T3STY
    wrote on last edited by
    #1

    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!

    1 Reply Last reply
    0
    • T Offline
      T Offline
      t3685
      wrote on last edited by
      #2

      You could use template functions.

      1 Reply Last reply
      0
      • T3STYT Offline
        T3STYT Offline
        T3STY
        wrote on last edited by
        #3

        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!

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved