Unfortunately the question has been changed and now is asking something else completely different from the original, I will leave the original answer below
I understand that you are learning but what you want to do requires state and can not do so trivial. William Nascimento’s response code works if you just run this isolated example. If you are going to use within a more complex application, you will have problems.
You will have to store the situation in some variable and this variable will have to be known by its function in isolation. If you use a global variable you will have problems (see more here, here and here).
You can create a class or at least a structure to do this but actually as it is not enough to assemble a new data structure not need so much, can do something simpler, something like that:
#include <iostream>
int getNumber(bool &check) { //recebe o estado por referência
if (check) {
check = false;
return 10;
} else {
check = true;
return 20;
}
}
int main() {
bool numero = false;
std::cout << getNumber(numero) << endl;
std::cout << getNumber(numero) << endl;
std::cout << getNumber(numero) << endl;
}
Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.
I know you’ll find it more complicated. But what good is something simple and wrong? This way you can have as many states as you want. If you don’t do so or the function becomes extremely useful or you will have to coordinate the use of it, which is much more difficult than doing this.
I used the parameter by reference so any change in the variable check
within the function also changes the variable that sent the value to the function, then the variable numero
is always in the right state for that moment.
If you could verify whether a variable was created or not, its logic would not work unless you had the variable deleted, which makes no sense. The very title of your question makes no sense with the actual current question.
Note that on ideone I improved your code.
I made a more sophisticated example. I do not recommend using the third form since it uses a static variable, ie global state. But at least it’s encapsulated in a function, it already helps.
#include <iostream>
using namespace std;
template <typename T>
class Interleave {
T a, b;
bool check = false;
public:
Interleave(T a, T b) {
this->a = a;
this->b = b;
}
T getValue() {
check = !check;
return check ? a : b;
}
};
int getNumber() {
static Interleave<int> numero(10, 20);
return numero.getValue();
}
int main() {
Interleave<int> numero(10, 20);
cout << numero.getValue() << endl;
cout << numero.getValue() << endl;
cout << numero.getValue() << endl;
Interleave<string> texto("aaa", "bbb");
cout << texto.getValue() << endl;
cout << texto.getValue() << endl;
cout << texto.getValue() << endl;
cout << getNumber() << endl;
cout << getNumber() << endl;
cout << getNumber() << endl;
}
Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.
C++ is a compiled and static typing language. All variables must exist at compile time. Unlike what you are used to with dynamic languages, it is not possible to treat variables as data directly.
Remember of that question? There is a hint to simulate the dynamic variables. In the background the dynamic languages make more or less that map that I pointed out to you. When you are programming you have the illusion of being using variables but in the background you are accessing positions in these maps also called dictionaries or hashes.
Have you noticed why these variables are dynamic and may or may not exist? Because they are given in key pairs value, where the key is the variable name and the value is what is stored in this "variable".
Even some dynamic languages may have real variables. In general local variables are considered real. These variables must also always exist. These variables are preferable because they are more guaranteed by the compiler and are faster.
If you really have a problem that the variable may or may not exist, use a map and simulate the variable. In C++ there is no syntax that facilitates access as if it were a truth variable.
But you can probably do the same thing another way. When you’re programming in static languages you have to think in a different way.
Guilherme Nascimento’s answer is correct, but these variables that he is talking about only exist in the compilation process, they have no use in the application itself. They are used to help compile the application, not to run it. And their use is discouraged whenever possible (not always). Think of two different languages that are in the same code but do not communicate. Preprocessor variable are even part of C++.
If you still think you need this, ask a question saying where you want to go, maybe we have a better solution.
I’ve told you not to change the question you asked before. And to put examples, detail what you want from the beginning. You may be thinking it’s the same question. But when you put what you want to do, you changed the question completely. The answers given before no longer serve because you originally asked one thing and you wanted to know something else.
– Maniero