473,385 Members | 1,780 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

access to a static variable within a function

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
4 1799
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Gernot Frisch | last post by:
Problem: A.cpp: ------ static FOO* gFoo=NULL; A.h extern FOO* gFoo; gives: L2001 - unresolved external: "symbol struct FOO* gFoo"
12
by: codefixer | last post by:
Hello: I am trying to understand the use of static in this program. http://nanocrew.net/sw/nscdec.c for "inverse" matrix. What difference would it make if it were not static and just "const...
5
by: Ben S. Terry | last post by:
Can someone please tell me how to declare a static variable in a function? The following generates a compiler error: public class MyClass { public void MyFunc() { static int i; // Compiler...
12
by: Steve Blinkhorn | last post by:
Does anyone know of a way of accessing and modifying variables declared static within a function from outside that function? Please no homilies on why it's bad practice: the context is very...
18
by: Jack | last post by:
Thanks.
55
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in...
14
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object declared static instead classes 4. objects declared...
0
MMcCarthy
by: MMcCarthy | last post by:
We often get questions on this site that refer to the scope of variables and where and how they are declared. This tutorial is intended to cover the basics of variable scope in VBA for MS Access. For...
8
by: aarklon | last post by:
Hi all, see:- http://linuxgazette.net/issue51/pramode.html
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.