Search code examples
c++functionpointersfunction-pointersdefault-parameters

assigning overloaded function to function pointer as default value


for a function

foo( int (*fnptr)(int) );

I want to put a default value for the function pointer int bar(int)

ie the default value of the pointer is bar

bar is also overloaded as

double bar (double);
bool bar (bool);

how can I assign the value??

I tried

foo ( int (*fnptr)(int) = bar);

but it doesn't work.

EDIT I'm using MS visual studio and getting error code C2440

'default argument': cannot convert from 'overloaded-function' to 'Error_C (__cdecl *)(HMstd::exception)'

My actual function is a member function of class i defined exception of namespace HMstd

virtual Error_C execute_protocol(Error_C(*execute)(exception ex) = HMstd::MErr);

And the function is

Error_C MErr(Error_C code);
Error_C MErr(char* desc);
Error_C MErr(exception ex);

where Error_C is another class

This is the definition of the three overloaded function HMstd::MErr is

Error_C HMstd::MErr(Error_C code)
{
    std::cout << "\n\nError: An Error Of Code " << int(code) << "     Occured....\n\n";
    return SUCCESS;
}

 Error_C HMstd::MErr(char* desc)
{
    if (desc == NULLPTR)
        return E_NULLPTR;
    std::cout << desc;
    return SUCCESS;
}

Error_C HMstd::MErr(exception ex)
{
    bool Nullar = TRUE;
    bool uninit;
    for (int i = 0;i < 200;i++)
        if (ex.description[i] != '\0')
            Nullar = FALSE;
    uninit = (int(ex.code) == -201) && Nullar;
    if (uninit)
    {
        return UNINIT_PARAMETER;
    }
    MErr(ex.code);
    MErr(ex.description);
    return SUCCESS;
} 

Solution

  • The Answer was very simple. I had declared the Function

    Error_C MErr (exception ex);
    

    after declaration of class exception

    so the virtual member function of class exception cannot use this particular overload of the function MErr i have no way to implement this default parameter.