Click here to Skip to main content
15,891,136 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: I just spent two days on nothing, but all is not lost! C# to the rescue Pin
Kenneth Haugland18-Jul-21 6:38
mvaKenneth Haugland18-Jul-21 6:38 
GeneralRe: I just spent two days on nothing, but all is not lost! C# to the rescue Pin
honey the codewitch18-Jul-21 6:41
mvahoney the codewitch18-Jul-21 6:41 
GeneralRe: I just spent two days on nothing, but all is not lost! C# to the rescue Pin
Kenneth Haugland18-Jul-21 11:53
mvaKenneth Haugland18-Jul-21 11:53 
GeneralRe: I just spent two days on nothing, but all is not lost! C# to the rescue Pin
Dan Neely19-Jul-21 3:20
Dan Neely19-Jul-21 3:20 
GeneralRe: I just spent two days on nothing, but all is not lost! C# to the rescue Pin
OriginalGriff18-Jul-21 5:18
mveOriginalGriff18-Jul-21 5:18 
GeneralRe: I just spent two days on nothing, but all is not lost! C# to the rescue Pin
Greg Utas18-Jul-21 7:04
professionalGreg Utas18-Jul-21 7:04 
GeneralRe: I just spent two days on nothing, but all is not lost! C# to the rescue Pin
swampwiz18-Jul-21 11:46
swampwiz18-Jul-21 11:46 
GeneralRe: I just spent two days on nothing, but all is not lost! C# to the rescue Pin
honey the codewitch18-Jul-21 12:28
mvahoney the codewitch18-Jul-21 12:28 
Edit: I may have misinterpreted you. If you meant you don't get how they work in C# then disregard this comment! Smile | :)

It's a little easier these days with lambdas in C++. If you don't capture anything it decays to a standard function pointer meaning you can declare callbacks inline

the weird thing about function pointers in c/c++ is declaring them

C++
the_return_type(*the_callback_variable_name)(parameter_type1,parameter_type2,...);


so like
C++
void*(allocator)(size_t)=malloc;


That would declare a function pointer called allocator and make it point to malloc.

But once you've declared it using it is basically like using any other function:
C++
// allocate 10 bytes
void* buf = allocator(10);


There's your "callback" Smile | :)


Now as far as a function that takes one of those monsters as a parameter type - in other words, a function that takes a callback, you can declare that parameter inline in a function like you can in C#, except that you can't capture with it (unless you use std::function which is different):

C++
static void do_my_func_with_callback(const char* sz,bool(*callback)(const char*)) { 
  // returns a bool but we don't use it here
  callback(sz);
}


and then

C++
do_my_func_with_callback("something",[](const char* sz){ printf("called with %s\e\n"); });

Real programmers use butterflies


modified 18-Jul-21 19:26pm.

GeneralRe: I just spent two days on nothing, but all is not lost! C# to the rescue Pin
swampwiz19-Jul-21 8:23
swampwiz19-Jul-21 8:23 
GeneralRe: I just spent two days on nothing, but all is not lost! C# to the rescue Pin
Gary R. Wheeler18-Jul-21 14:38
Gary R. Wheeler18-Jul-21 14:38 
GeneralRe: I just spent two days on nothing, but all is not lost! C# to the rescue Pin
swampwiz19-Jul-21 8:25
swampwiz19-Jul-21 8:25 
GeneralWisdom with my tea Pin
W Balboos, GHB18-Jul-21 1:09
W Balboos, GHB18-Jul-21 1:09 
GeneralRe: Wisdom with my tea Pin
peterkmx18-Jul-21 4:12
professionalpeterkmx18-Jul-21 4:12 
GeneralRe: Wisdom with my tea Pin
Mike Hankey18-Jul-21 5:28
mveMike Hankey18-Jul-21 5:28 
GeneralRe: Wisdom with my tea Pin
Nelek18-Jul-21 8:20
protectorNelek18-Jul-21 8:20 
GeneralRe: Wisdom with my tea Pin
Mike Hankey18-Jul-21 8:41
mveMike Hankey18-Jul-21 8:41 
GeneralRe: Wisdom with my tea Pin
Cp-Coder18-Jul-21 13:30
Cp-Coder18-Jul-21 13:30 
GeneralRe: Wisdom with my tea Pin
den2k8818-Jul-21 21:24
professionalden2k8818-Jul-21 21:24 
GeneralRe: Wisdom with my tea Pin
W Balboos, GHB19-Jul-21 0:45
W Balboos, GHB19-Jul-21 0:45 
GeneralRe: Wisdom with my tea Pin
Matt T Heffron23-Jul-21 14:40
professionalMatt T Heffron23-Jul-21 14:40 
GeneralLate to the party... Pin
David O'Neil17-Jul-21 20:23
professionalDavid O'Neil17-Jul-21 20:23 
GeneralRe: Late to the party... Pin
dandy7218-Jul-21 3:34
dandy7218-Jul-21 3:34 
GeneralI have an very old HP computer that can not power up now Pin
Southmountain17-Jul-21 17:11
Southmountain17-Jul-21 17:11 
GeneralRe: I have an very old HP computer that can not power up now Pin
Ron Anders17-Jul-21 17:31
Ron Anders17-Jul-21 17:31 
GeneralRe: I have an very old HP computer that can not power up now Pin
Dave Kreskowiak17-Jul-21 17:54
mveDave Kreskowiak17-Jul-21 17:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.