Hello. At the moment I am at the initial stage of learning C ++, so I would like to ask a more experienced question. I wrote a simple console program and tried to implement an elementary dialogue with the user by creating a special method in one of the classes:
class MyClass { public : void method() { cout << endl << "Method used" << endl; } void menu() { int* choice = new int; cout << "1. Use method;" << endl << "2. Stop;" << endl << "set : "; cin >> *choice; switch(*choice) { case 1 : delete choice; method(); menu(); break; case 2 : delete choice; exit(1); break; } } }; In fact, it is a bit bigger, but the concept is the same. Actually I wanted to ask about the application of this principle: is it ok to do this? I mean, I’m a little worried that each new entry into the menu () method after entering "1" will suspend the active call. Therefore, if you continue for so long, you will get a rather long chain of suspended calls.
Or is it more expedient to endure all this disgrace, for example, in main ()? :
int main() { MyClass* my_obj = new MyClass; int* choice = new int; while(true) { cout << "1. Use method;" << endl << "2. Stop;" << endl << "set : "; cin >> *choice; switch(*choice) { case 1 : my_obj->method(); break; case 2 : delete choice; delete my_obj; exit(1); break; } } return 0; } When using the menu () method, as well as main (), all allocated memory is freed. However, the long tail of the suspended calls remains. Can such a chain, for example, sooner or later take a lot of memory or something like that? In general, I would like to receive a reasoned answer, thanks =)
StackOverflow, that is, a stack overflow. An infinite loop is a common approach in development. For example, the life of any windows-application is in the loop window processing. - teranchoicein a pile? Why not justint choiceand go everywhere without*? - VladDwhile/for/if/ ... - VladD