6

I would like the following simple function to call the function that called it, however the function is called by multiple functions, so it needs to recognize which function called it specifically and then call it.

int wrong()
{
    std::cout << "WRONG \n";
    return 0;
}

As a follow up, is this the sort of function that would be better expressed as a void?

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
ticklemyiguana
  • 165
  • 1
  • 9
  • 3
    There is no standard way of determining which function called you. Maybe your environment has a way of getting the current call stack, but again, that would be non-standard. – PaulMcKenzie Dec 05 '15 at 01:08
  • Generally speaking, you don't want to do this. You may end up in a recursive loop. What problem are you actually trying to solve? – M.M Dec 05 '15 at 01:09
  • I am trying to write a simple test - like a test that the user takes. Every time they answer incorrectly, I want this function to be called, but instead of just going back to main, thus reading the next question, I want it to go back to the question that the user got wrong. – ticklemyiguana Dec 05 '15 at 01:10
  • 2
    You need to arrange your code in the previous function so that it calls this function and then, depending on the return value, selects which question to ask – M.M Dec 05 '15 at 01:16
  • @ticklemyiguana Your request is for program design. Usually the reason (maybe the only legit reason) one wants the calling function is simply for logging purposes, not for business logic. – PaulMcKenzie Dec 05 '15 at 01:19
  • @ M.M I love it. That's incredibly simple. Thank you. – ticklemyiguana Dec 05 '15 at 01:19
  • 7
    I propose we nominate this question as best example for a XY problem. – Quentin Dec 05 '15 at 01:50
  • I am sure the OP is dealing with a problem that can be solved in a whole different way from the question. However if the OP has to do that whatsoever, on the language layer, using a pointer to function would be the simplest way. – Dean Seo Dec 05 '15 at 02:33
  • @M.M Can we have that as an answer please. – Emil Laine Dec 05 '15 at 02:50
  • @zenith it's kinda difficult to write more than I just wrote already , since we don't really have any code.. I'd basically be making up a demo program from scratch – M.M Dec 05 '15 at 04:47

1 Answers1

3

What you want is a callback. Callbacks are implemented like this in C++:

typedef int (*CallbackType)( char c );  

int wrong( CallbackType callback )
{
    std::cout << "WRONG \n";
    int r = callback( 'x' );
    return r;
}

int also_wrong( char c )
{
    return wrong( also_wrong );
}

Of course this will result in runaway recursion, so it will get you into a lot of trouble, but it definitely answers your question.

And yes, if all it does is to return 0, then this is the sort of function that would be better expressed as returning void.

Mike Nakis
  • 56,297
  • 11
  • 110
  • 142
  • Interesting. There are a few things in there that I don't know yet - I'm quite new to C++ and programming in general, but I'll look that stuff up. – ticklemyiguana Dec 05 '15 at 01:17
  • 1
    This isn't the best solution to OP's problem – M.M Dec 05 '15 at 01:25
  • 1
    The OP might also have problems with his mortgage. We are not responsible for that. He asked a question, I answered precisely that, I even went as far as to include a warning as to what will happen if he uses the answer. I think I covered him. – Mike Nakis Dec 05 '15 at 01:27
  • 7
    Your code sample is bad. Firstly `main` must return `int` in C++. Secondly, in C++ it is not permitted to call the `main` function yourself. – M.M Dec 05 '15 at 01:36
  • From an educational point of view, you are right. From a factual point of view, I think not; I think in C++, just as in C, you can declare `main` anyway you like. But I will fix it. (Also, nothing prevents you from calling it. And apparently the OP wants to do exactly that.) – Mike Nakis Dec 05 '15 at 01:39
  • 4
    @mike in C++ it is not legal to call main. It may work in some compilers and systems, but that just means your code breaks when your compiler updates, changes, you get a new library, different target, or a myriad of other reasons. Only use undefined behavior in the cases where it is worth it, and a toy SO example is not one of them. – Yakk - Adam Nevraumont Dec 05 '15 at 02:12
  • [A detailed answer](http://stackoverflow.com/a/2128727/1227469) as to why calling main is not legal. – JBentley Dec 05 '15 at 02:13
  • @JBentley (and @Yakk) thanks. My mistake. Now I need to see what I must do to correct this. – Mike Nakis Dec 05 '15 at 02:15