user2086751
user2086751

Reputation:

How to use a variable globally without declaring it as a global?

I have a switch statement like this

switch(option)
{
case 1:
{
    int randomVariable;
}
case 2:
{

}
case 3:
{
\\ I want to use randomVariable here but it is not letting me since it is not in the same scope
}

}
}

Any Ideas on how to solve this issue? Please note that randomVariable must be declared under case 1: and randomVariable is an array. Please try to stick to std libraries and please please please do not use vectors as this is for a project and vectors are not at all discussed in this class.

Upvotes: 1

Views: 226

Answers (5)

LihO
LihO

Reputation: 42085

The region in which you are able to access a variable is called its scope. For local non-static variables the boundaries of this scope are defined with curly braces ({ ... }), so:

{
    ...
    int a = 0;
    switch (option)
    {
        case 1:
        {
            int b;
            a = 2;
        } // <-- the scope of b ends here
        ...
    }
} // <-- the scope of a ends here

Please note that randomVariable must be declared inside of case 1: as this is the option that creates the array

Since you are programming in C++, instead of C-style array use std::vector, which is the container holding elements within the continuous block of memory just like an array, but it can change in size:

#include <vector>
...
{
    std::vector<int> myVector;
    switch (option)
    {
        case 1:
        {
            int size;
            // value retrieved in run-time is assigned into size here
            myVector.resize(size, 0);
        }
        case 2:
        {
            // you can use your vector here:
            if (myVector.size() > 3)
                myVector[2] = 7;
        }
        ...
    }
} // <-- end of myVector's scope

in this example myVector.resize(size, 0); resizes the memory internally used by vector so that it's big enough to hold size elements and if its size has been increased it also inserts new elements to this memory and initializes them to 0. What is important here is that myVector is an object with automatic storage duration, which means that when the execution goes out of the scope where it has been defined, the memory is cleaned up automatically. This saves you from the ugly memory management that you would have to take care of in case you use dynamically allocated C-style array.

Upvotes: 3

Aiias
Aiias

Reputation: 4748

Edit:

After your comments, for your use case, you probably want something like this:

int * myArr = 0;
int myArrSize = 10;

switch (option) {
  case 1:
    if (myArr != 0) {
      // Clean up memory if we are re-initializing
      delete [] myArr;
    }
    myArr = new int[myArrSize];
    break;
  case 2:
    break;
  case 3:
    int test = myArr[1];
    break;
}

// Clean up memory when we are done with the array
delete [] myArr;

Upvotes: 1

Keval Langalia
Keval Langalia

Reputation: 1892

here you go...

  • Make a new class(say for ex. superClass).
  • Create a stub of property field named randomVariable with getter & setter method.
  • make that property as static. (so that it's value can be used Globally same, even with different object differences).
  • Inherit this class in the class where you put your switch statements.
  • Now you can initialize & use randomvariable wherever you want by setting it's new or old value.

Hope this will help..!!!

Upvotes: 0

Tuxdude
Tuxdude

Reputation: 49473

A variable's scope is only within the block it is defined, so no it is not possible to define the variable in one scope i.e. within case 1: and access it outside the scope, unless you intend to combine the cases for 1 and 3 together without an explicit block { ... }.

Define the randomVariable in a scope visible throughout the switch statement, so that any case can access this variable.

{
    int randomVariable;
    switch(option)
    {
        case 1:
            {

            }
        case 2:
            {

            }
        case 3:
            {
                \\ I want to use randomVariable here but it is not letting me since it is not in the same scope
            }

    }
}

The scope above the switch could be a function, or another block depending on your code.

Upvotes: 0

AndiDog
AndiDog

Reputation: 70108

With your restriction that it has to be initialized in case 1, then just make the variable a pointer and use variable = new something(); in case 1.

By the way, this scope has nothing to do with "global".

Upvotes: 1

Related Questions