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;
} 
6
  • 2
    What error are you getting ? It looks fine to me. Commented Dec 5, 2016 at 14:00
  • 1
    I have no problem compiling such code - Could you provide an MCVE? Which compiler/version/platform are you using? Commented Dec 5, 2016 at 14:01
  • 2
    Note that you need to declare a return type for foo. Commented Dec 5, 2016 at 14:02
  • 1
    In particular, what compiler are you using? Commented Dec 5, 2016 at 14:02
  • 3
    Work as expected here. Commented Dec 5, 2016 at 14:09

3 Answers 3

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?

Sign up to request clarification or add additional context in comments.

14 Comments

in c++ better use static_cast
I afraid static_cast being "too advance" for such a simple question. Anyway, I will update it.
well static_cast is even more adequate here as it holds us from doing something that may cause UB :)
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 That's why I put "quote/end-quote"
|
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]

1 Comment

i have just started looking at lambda functions. i cant understand the code very much sorry
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.