-2

Is it possible to send control from other file to main file using GOTO statement if yes please tell. if is it not possible please tell me another method.

main.cc

{
    outer:                   // label where I want to return 
    callingfunc()
    //control go into calling func
}

source.cc //another source file with a class

class myclass
{
    public:
    callingfunc();
};

callingfunc()
{
    some code 
    goto outer;               //from here I want to send control to outer label in file **main.cc**
}
arulmr
  • 8,620
  • 9
  • 54
  • 69
Zubair
  • 1
  • 1

1 Answers1

0

You can only goto labels in the same function, and it's very poor practice to break a function across source files (which is only possible if an #included file contains the first part of the function). To transfer execution, you normally want to use function calls (whether to a hardcoded function or one identified by e.g. a function pointer or std::function<>) and return statements, sometimes throw and exceptions, and very very rarely something unusual like setjmp/longjmp (if you need to ask this question you shouldn't be playing with those latter functions). You should explain more about your program - ideally post some code - if you want specific advice on what suits your needs.

Update: now you've posted some code, you could consider something like this...

// source.h
enum Flow { Outer, Continue }; // whatever names make sense to you...
Flow callingfunc();

// main.cc
#include "source.h"
if (callingfunc(...) == Outer) goto outer;

// source.cc
#include "source.h"
Flow callingfunc() { ...; return Outer; ... return Continue; }

It's best to try to find a better name than "Outer"... something that communicates the condition that callingfunc has found or the consequent processing it's recommending (e.g. Timeout, Can_Retry, Model_Unsolvable).

Tony Delroy
  • 102,968
  • 15
  • 177
  • 252
  • Mixing C++ with `setjmp` and `longjmp` has potential to go even more wrong than using this in a "regular" C program. – Mats Petersson Aug 12 '14 at 07:45
  • @MatsPetersson: absolutely... only C++ experts with a lot of time to analyse their use case should even consider that and then only *in extremis* (e.g. for a non-portable coroutine implementation).... I only mention them for completeness. – Tony Delroy Aug 12 '14 at 07:47
  • @TonyD `throw` and exceptions in general should not be used to handle the program flow. – edmz Aug 12 '14 at 08:35
  • @black: 1) there's nothing in the question communicating the circumstances under which this transition's desired, and 2) for some value of "in general" sure, but programmers should still do whatever is functionally necessary or net best in their circumstances after giving appropriate weight to the "surprise"/confusion/training/documentation/etc costs involved with atypical usage. – Tony Delroy Aug 12 '14 at 08:42