Chrysaor
asked on
How to declare a global variable inside a function?
How do I declare a global variable inside a function? (so that it can be used by other functions)
>>How do I declare a global variable inside a function? (so that it can be used by other functions)
Only by having the function return a reference or pointer to the variable.
Only by having the function return a reference or pointer to the variable.
Do you really need it to be global, or just be able to use it in other functions?
You can't define a truly global variable a function, although by declaring it as static you can make it persist past the life of the function. Then you can return a pointer or reference to it as peetm suggested.
int *my_func() {
static int foo;
.
.
.
return &foo;
}
Because foo is declared as static, it lives on after the function returns.
You can't define a truly global variable a function, although by declaring it as static you can make it persist past the life of the function. Then you can return a pointer or reference to it as peetm suggested.
int *my_func() {
static int foo;
.
.
.
return &foo;
}
Because foo is declared as static, it lives on after the function returns.
You could even do something like this to restrict usage of such a global variable to a given set of functions, i.e.:
> class A
> {
> static bool Flag; // declare the global flag
> friend void foo(); // allow 'foo' to access the flag
>};
>bool A::Flag = true; // instantiate the flag
void foo()
{
A::Flag = true; // OK
}
void bar()
{
A::Flag = true; // Error: no access to private member
}
ZOPPO
> class A
> {
> static bool Flag; // declare the global flag
> friend void foo(); // allow 'foo' to access the flag
>};
>bool A::Flag = true; // instantiate the flag
void foo()
{
A::Flag = true; // OK
}
void bar()
{
A::Flag = true; // Error: no access to private member
}
ZOPPO
ASKER
Basically, I have a function which it returns an int (let's say int number(); ) I have another one function, let's call it void body(); . When the body() is called, it creates a vector like this: vector<int> P(number()), and assign values to the vectors e.g P[0] =1; P[1] = 7; etc etc. Then , I want from another function to recall the values of e.g P[0],P[1] etc..
That's why I asked if it was possible to declare vector<int> P(number) a global variable. Any suggestions on how to solve my problem?
Thanks a lot, sorry for your time.
That's why I asked if it was possible to declare vector<int> P(number) a global variable. Any suggestions on how to solve my problem?
Thanks a lot, sorry for your time.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Addition: the second possibility is more efficient since there's no need to create a copy of the vector ...
>> the second possibility is more efficient since there's no need to create a copy of the vector
Modern compilers support Named Return Value Optimization (NRVO), so 1 and 2 are likely to be optimal solutions.
http://msdn.microsoft.com/en-us/library/ms364057.aspx
Modern compilers support Named Return Value Optimization (NRVO), so 1 and 2 are likely to be optimal solutions.
http://msdn.microsoft.com/en-us/library/ms364057.aspx
that's not possible (at least not in C/C++). You have to declare that variable at the global (or a namespace) scope.
ZOPPO