By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,194 Members | 854 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,194 IT Pros & Developers. It's quick & easy.

access to a static variable within a function

P: n/a
Hello everybody. I've been having a look to the c++ FAQ but couldn't
get any help there.

Does anybody know if is it possible to access a static variable
declared within a function?.

Something like this:

#include <iostream>

void f(int a){
static int sum=0;
sum+=a;}

int main(){

f(8);
f(10);
std::cout<<f::suma; //This doesn't work. another suggestion?

}

Thank you,
J.

Jun 28 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On 28 Jun 2006 04:06:28 -0700, ja*********@gmail.com wrote:
Hello everybody. I've been having a look to the c++ FAQ but couldn't
get any help there.

Does anybody know if is it possible to access a static variable
declared within a function?.


A variable declared within a function belongs privately to this
funtion, static or dynamic. So the only way to see a static variable
from withina function would be only in the case of the function itself
returning a reference to the *static* variable.

You may want to take a look at "functors" (function-like objects,
which may be more adeuqate to whatever you are thinking of.

Best regards,

Zara
Jun 28 '06 #2

P: n/a
J. posted:
#include <iostream>

void f(int a){
static int sum=0;
sum+=a;}

int main(){

f(8);
f(10);
std::cout<<f::suma; //This doesn't work. another suggestion?

}

Maybe something along the lines of:

class f {
public:

static int sum;

f( int a )
{
sum += a;
}

};

int f::sum = 0;

#include <iostream>
#include <cstdlib>

int main()
{
f(8);
f(10);

std::cout << f::sum << '\n';

std::system("PAUSE");
}

--

Frederick Gotham
Jun 28 '06 #3

P: n/a
ja*********@gmail.com wrote:
Hello everybody. I've been having a look to the c++ FAQ but couldn't
get any help there.

Does anybody know if is it possible to access a static variable
declared within a function?.
Why shouldn't it?
Something like this:

#include <iostream>

void f(int a){
static int sum=0;
sum+=a;}

int main(){

f(8);
f(10);
std::cout<<f::suma; //This doesn't work. another suggestion?

}


Ah, you want to access it from somewhere else than the function itself.
That's not possible. After all, it's a local variable, and usually, you
define it in the function just because you only need access to it from that
function.

Jun 28 '06 #4

P: n/a
Hi.
std::cout<<f::suma; //This doesn't work. another suggestion?


class summation
{
public:
static add( int a ) { sum += a; }
static int sum; // Is initialized to zero because it is static
};

int main( int, char** )
{
summation x;
x.add(8);
x.add(10);
std::cout << x.sum << std::endl;
return 0;
}

But I am not sure if this is what you WANT to do...

Regards,

Marco.
Jun 30 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.