Usage of enum - global and local [duplicate] - c++

This question already has an answer here:
How to access a global variable within a local scope?
(1 answer)
Closed 6 years ago.
I just tried it to use globaly defined enum with same name as locally defined.
I had initiallized with same values except the start point.
enum Day {Sunday = 1 ,Monday,Tuesday,Wednesday,Thursday,Friday /*= 15 */,Saturday};
void enumUse() {
enum Day {Sunday = 2 ,Monday,Tuesday,Wednesday,Thursday,Friday /*= 15 */,Saturday};
Day today = Tuesday;
cout<<Sunday<<endl;
cout<<Monday<<endl;
cout<<Tuesday<<endl;
cout<<Wednesday<<endl;
cout<<Thursday<<endl;
cout<<Friday<<endl;
cout<<Saturday<<endl;
cout<<"Today: "<<today;
cout<<endl;
}
This code snippet gives the output of local enum.
If comment it out, it gives output related to global one.
How could I use global enum without commenting local enum.

You can use the scope resolution operator, because the global Day is defined in global scope
//'Day' from global scope
::Day today = ::Tuesday; //'Tuesday' also needs it because it would take the local one,
//which can't be assigned to the global 'Day' (they're different
//enums after all)

Related

What is the scope and evaluation of this if [duplicate]

This question already has answers here:
Declaring and initializing a variable in a Conditional or Control statement in C++
(9 answers)
Defining a variable in the condition part of an if-statement?
(5 answers)
Closed 1 year ago.
When cleaning up some code that I have found online, I came across with this weird c++ line:
if (int i = 1) std::cout << i;
With LLVM it compiled fine and the console output is 1, but how does the scope is handled in here, shouldn't the i variable be only accessible inside the conditional (inside the parenthesis)? And how is that possible to be evaluated to true, isn't an assignment a void operation and with no value, so 0/false? What is going on with this line?

Local Scope Resolution [duplicate]

This question already has answers here:
Why are global variables always initialized to '0', but not local variables? [duplicate]
(4 answers)
Why are global and static variables initialized to their default values?
(5 answers)
Closed 1 year ago.
I decided to change the scope of a function operator from global to local. After changing from the commented code, I found that my code no longer runs and exits with error:
C4700 uninitialized local variable 'n' used.\
This seems to be a quite obvious contradiction to the actual method of local resolution. Does anyone have an explanation for this?
int Combs::factorial(int a)
{
//value = 1;
int n;
for (int i = a; i >0; i--)
{
n *= i;
}
cout << n;
return n;
}
When the variable is declared at global/file scope, the compiler initializes it for you, when it's local to a function, it doesn't, so you need to do it yourself.
n is indeed used unitialized, when it was a global variable it was not.

string declaration before main funciton in C++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
i'm new to understanding stri and declaring them. can a string variable = "" ;
i'm confused why you could put a equal sign on a declaration also. is this to declare a function to be empty sort of like a global variable? like int variable = 0; is that sort of like a global string variable? once i change it it'll be stored down in main function and other fucntions?
#include<iostream>
using namespace std;
string variable = "";
int main() {
return 0;
}
The statement you're asking about defines a variable in
namespace scope (in this case, the global namespace). It's more
or less like any definition: except with regards to linkage and
lifetime:
The lifetime is static, which means that the variable will be
initialized before main is entered (in practice, anyway),
and will be destructed after exit is called.
The linkage is external, which means that the name variable
always refers to the same entity, even in different translation
units.
And of course, since the scope is namespace scope, the variable
can be directly referred to from any function in that namespace,
or in any namespace nested in that namespace. And since all
other namespaces are nested in the global namespace, that means
that the variable can be referred to everywhere (unless some
other declaration is hiding it).
You can assign value to a string variable in two ways.
string variable = "";
This means that you declared a variable named variable of type string and you assigned an empty string to this variable.
You can also assign value this way
string variable("");
this means exactly the same as the one above. but here you don't need the equal sign.
Here's a reference which can help you understand strings http://www.cprogramming.com/tutorial/string.html
There are four separate parts to the small example you posted:
1) Include the necessary headers:
#include<iostream>
2) Declare any used namespaces:
using namespace std;
3) Now this is what you are probably asking about:
string variable = "";
This line above is completely independent of the function below. It is declaring a global variable called variable, of type string. It is also assigning a value to it, which just happens to be the empty string i.e. "". You could also have said instead:
string variable = "My name is numLOCK";
So you are just initializing the value of that variable to the specified text assigning aome value to the variable.
4) Next comes the main function. This does the actual work.
int main() {
return 0;
}
Note that you can use the global variable declared above in your main or any other function. You can also declare more variables inside the function. For example:
string someText = "My name is numLOCK";
int main() {
int someNumber = 42;
cout << someText << endl;
cout << someNumber << endl;
return 0;
}
This will print out:
My name is numLOCK
42

Are variables declared in loops visible to rest of the code? [duplicate]

This question already has answers here:
What is the scope of a 'while' and 'for' loop?
(10 answers)
Closed 9 years ago.
I was reading this book and it said :
Variables declared in a scope aren't visible outside it.
It also said :
Scopes are declared by 2 curly braces - like a block of code.
So, if I had a situation like this :
for(_statement1_)
{
int var;
/*code*/
}
cout << var << " number of rockets left.\n";
Would the value printed be the same as the value of the var declared in the loop?
Thank you
As your book says, the variable is scoped inside the loop's block, and isn't visible outside it.
Your code won't compile unless there is a different variable var outside the loop's scope. If there is, then the final statement would use that, not the one in the loop which is now out of scope.
No, you can not use that variable outside the loop. var is visible only inside the loop.
It will be a compiler error since the scope of var limited inside for loop only.

Initialization List Problems [duplicate]

This question already has an answer here:
Initializing members with members
(1 answer)
Closed 8 years ago.
DoubleVector::DoubleVector(unsigned int buffer) : len(buffer), data(new base_int[len]), start(len / 2), end(start)
This produces a very large values for both start and end even though the buffer is set to 50. len contains the correct value of 50 but start and end both contain some value over a million. I then changed to code to the following.
DoubleVector::DoubleVector(unsigned int buffer) : len(buffer), data(new base_int[len]), start(buffer / 2), end(start)
Now both start and end were initialized with the correct values of 25. Why? Are you not supposed to assume there is any order in which the variables will be initialized?
The order of base member initialisation is the order that the member variables appear in the class definition.
It's best not to rely on that. (As to do so makes code very brittle).