2

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;
} 
WARhead
  • 643
  • 5
  • 17

3 Answers3

3

QUICK ANSWER:

Use type-cast

SHORT CODE:

// ...
int bar (int) {
  cout << "Right\n";
  // bar(true); // just in case you want to invoke bool bar(bool)
  // bar(0.0f);
  return 0;
}
// ...
int foo (int (*ptr) (int) = static_cast<int (*) (int)>(bar)) {
  return ptr(0);
}
// ...

FULL CODE:

#include <iostream>

using namespace std;

int bar (int) {
  cout << "Right\n";
  // bar(true); // just in case you want to invoke bool bar(bool)
  // bar(0.0f);
  return 0;
}

bool bar (bool) {
  return false;
}

double bar (double) {
  return 0;
}

int foo (int (*ptr) (int) = static_cast<int (*) (int)>(bar)) {
  return ptr(0);
}

int main () {
  return foo();
}

EXPLAINATION:

You have more than one bar so I cannot put = bar as default parameter. Because of this, you must specify which bar. I used type-casting so the compiler can specify one of these bar. I seen that you provide only two bar (bool bar(bool) and double bar(double), but you cannot convert any of these function to int bar(int) (if gcc allows it, program possibly works improperly, especially with double bar(double)), so you need to call one of these two in the new int bar(int)

NOTE:

You can also use unsafe C-Style type-casting (int (*)(int)) bar instead of static_cast<int (*) (int)>(bar) but this is C++

If you're using Turbo C++, the code above probably won't work, so you may prefer C-style type-casting, or just switch to GCC.

SEE ALSO:

How do I specify a pointer to an overloaded function?

Community
  • 1
  • 1
DMaster
  • 603
  • 1
  • 10
  • 23
  • 1
    in c++ better use `static_cast` – W.F. Dec 05 '16 at 14:07
  • I afraid `static_cast` being "too advance" for such a simple question. Anyway, I will update it. – DMaster Dec 05 '16 at 14:09
  • 3
    well `static_cast` is even more adequate here as it holds us from doing something that may cause UB :) – W.F. Dec 05 '16 at 14:13
  • There's nothing advanced about learning to prefer c++ styled casts. Using a chisel instead of a sledgehammer is something all novice programmers should learn as soon as possible. – StoryTeller - Unslander Monica Dec 05 '16 at 14:24
  • @StoryTeller That's why I put "quote/end-quote" – DMaster Dec 05 '16 at 14:25
  • @DMaster _"you provide only two bar_ `bool bar(bool)` and `double bar(double)`," i said _also overloaded as_ .., that means that apart from `int bar (int)` it is also overloaded as that – WARhead Dec 05 '16 at 15:53
  • @WARhead Sorry then! I think you still need to add `int bar(int)` to avoid confusion. Also, if you did that but still get an error message, that weird! Which compiler are you using and how do you invoke your compiler (e.g. `g++ main.cpp -o main`)? – DMaster Dec 05 '16 at 15:57
  • @WARhead I'm not familiar with Win32 APIs, tell me: Is `HMstd::MErr` a static function? What's its type? Is it a macro? – DMaster Dec 05 '16 at 16:14
  • @DMaster no this is not a Win32 API `HMstd::MErr` is merely a function that displays the error code on the screen for debugging purposes and during run time error loging. `HMstd::MErr` is not a static function. what you see there is its declaration – WARhead Dec 05 '16 at 16:16
  • @WARhead It is not a standard function neither! I guess you wrote it, you might want to add your `HMstd` class `HMstd::MErr` function to your question – DMaster Dec 05 '16 at 16:19
  • @DMaster It is a namepace. Along with the class for which the function `execute_protocol` is declared – WARhead Dec 05 '16 at 16:21
  • Error C2440 occurs during runtime or compiler-time? (I'm not familiar with VS C++ neither) – DMaster Dec 05 '16 at 16:25
  • @DMaster compile time error[https://msdn.microsoft.com/query/dev14.query?appId=Dev14IDEF1&l=EN-US&k=k(C2440)&rd=true](error help) – WARhead Dec 05 '16 at 16:27
  • @WARhead Could you please add `static` before `HMstd::MErr` declaration and tell me what happens? – DMaster Dec 05 '16 at 16:31
1

Maybe you are looking for something like this:

#include <iostream>
#include <functional>

double bar (double) {
    std::cout << "double bar (double) called" << std::endl;
    return 0.0;
}
bool bar (bool) {
    std::cout << "bool bar (bool) called" << std::endl;
    return false;
}

void foo(std::function<int(int)> fn = [](int p) -> int{ return bar(static_cast<double>(p)); }) {
    fn(2);
}

int main() {
    foo();
}

Output:

double bar (double) called

[live demo]

you could also replace usage of the std::function with pointer to function if you desire by:

void foo(int (*fn_ptr)(int) = +[](int p) -> int{ return bar(static_cast<double>(p)); }) {
    fn_ptr(2);
}

[live demo]

W.F.
  • 13,888
  • 2
  • 34
  • 81
0

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.

WARhead
  • 643
  • 5
  • 17