-3

When ı use goto in class in c++ ,I faced to that message,how can ı use goto in class c++?

In member function `void note_system::student_no()':
  label 'stop' used but not defined`
bobah
  • 18,364
  • 2
  • 37
  • 70
Burak Deniz
  • 55
  • 1
  • 6

1 Answers1

1

You would do something like this (to have a single scope exit point, say when for some exotic reason using a RAII-based trick is not possible):

status_code func_with_goto() {
    if (i_see_errors()) goto func_end;
    ...
    func_end:
        return status_code::error;
}

Or like this (to emulate break-by-label-like behavior):

outer: while (...) {
    inner: while (...) {
        ...
        goto outer;
    }
}

Or if you just want to reimplement your second Basic program from 80-th in C++:

x10: std::cout << "Hello World!" << std::endl;
x20: goto x10;

There are very few cases where goto may be justified. Its presence complicates static code analysis by both humans and compilers. This does not mean at all that goto should be banned, it should just be use with double care.

bobah
  • 18,364
  • 2
  • 37
  • 70
  • I haven't seen a proper use for this in C++. In C, it's reasonable, but C++ has RAII. – chris Mar 09 '14 at 19:02
  • @chris goto is still useful, although most times, a proper loop does the same logic, but cleaner. RAII has nothing to do with gotos, in my opinion. – stefan Mar 09 '14 at 19:04
  • @stefan It surely does. It solves the problem that sometimes leads C programmers to use goto. – David Heffernan Mar 09 '14 at 19:05
  • @DavidHeffernan Well yes, if you use gotos to perform clean-up, then one should switch to RAII, but apart from that it's completely orthogonal. – stefan Mar 09 '14 at 19:06
  • 1
    I put double loops in another function and return out of them – paulm Mar 09 '14 at 19:06
  • @stefan When else is goto a good idea? – David Heffernan Mar 09 '14 at 19:07
  • @DavidHeffernan I personally have used them recently in a `switch`: http://pastie.org/8901481 – stefan Mar 09 '14 at 19:09
  • @stefan I'd reject that code immediately at a review – David Heffernan Mar 09 '14 at 19:11
  • 1
    @DavidHeffernan Well then, care to propose a better alternative? – stefan Mar 09 '14 at 19:12
  • @chris - the question was "how", not "when" :) – bobah Mar 09 '14 at 19:13
  • 1
    @DavidHeffernan I've had code where I've got a list of objects with fields a, b, c, d (already sorted by a and b), and where I had to process all records, with two levels of grouping, and set up some additional bits when I started working on a new group. The readable and intuitive flowchart didn't really translate to any construct better than `goto` with `NewA` and `NewB` labels. (And looking at stefan's code, I thought I had a simple suggestion, but that doesn't actually work in this case, so I'm interested too in knowing how you'd rewrite it.) –  Mar 09 '14 at 19:14
  • @hvd That's certainly a possibility, I don't know yet if I prefer it, but I can get the general idea to work. Thanks for proposing it! – stefan Mar 09 '14 at 19:20
  • @stefan Heh, I actually deleted my comment when I noticed that it would change the behaviour of your code, but if it's close enough for your needs, then I'm glad it was useful anyway. :) –  Mar 09 '14 at 19:23
  • @hvd in this case it is trivial to write `return Functor::call(argc, argv);` – David Heffernan Mar 09 '14 at 19:24
  • 1
    @DavidHeffernan Indeed. That causes the sort of unnecessary and unwanted code duplication, though, that I've always tried to avoid. For me personally, in that case, `goto` is easier to follow than your suggestion. –  Mar 09 '14 at 19:27
  • @hvd The duplication is marginal at most. It's a one liner return. Of exactly the same nature as many others in that code. And you can at least see that there is a direct return. For a more complex clause with multiple lines of code you would extract to a method. – David Heffernan Mar 09 '14 at 19:30
  • @DavidHeffernan I think we may have had this discussion before, and while I'm sure we can agree that code should be kept as readable as possible, and that `goto` generally decreases readability, we disagree on how much, and as a result will not be able to agree on when the benefits outweigh the costs. –  Mar 09 '14 at 19:35
  • @hvd These are always subjective to a degree. As gotos go, this is pretty borderline. Perhaps I'm too dogmatic. Then again, I think it can be dogmatic to remove all conceivable duplication. I don't find that return statement to violate DRY. You probably disagree. Which is just cool. – David Heffernan Mar 09 '14 at 19:38