| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"Joe" |
| Date: |
17 Jan 2005 01:56:06 PM |
| Object: |
static extern help |
I have 2 cpp modules (test.cpp, main.cpp). There is a static int
record_count variable declared globally in the test.cpp and incremented
in a function contained in the test.cpp module.
I would like to output the record_count value at the end of my main
function in main.cpp. How shoudl I declare this in main.cpp or is
there a better way to do this entirely?
Thanks!
.
|
|
| User: "Victor Bazarov" |
|
| Title: Re: static extern help |
17 Jan 2005 02:02:28 PM |
|
|
Joe wrote:
I have 2 cpp modules (test.cpp, main.cpp). There is a static int
record_count variable declared globally in the test.cpp and incremented
in a function contained in the test.cpp module.
'static' here refers to the storage duration then.
I would like to output the record_count value at the end of my main
function in main.cpp. How shoudl I declare this in main.cpp or is
there a better way to do this entirely?
Do not use the keyword 'static' in the declaration/definition. Use the
keyword 'extern'. Make sure that only one of the modules has that var
_defined_ (add an initialiser to it).
V
.
|
|
|
| User: "Joe" |
|
| Title: Re: static extern help |
17 Jan 2005 02:18:51 PM |
|
|
If I remove the "static" from the definition, how do I keep a running
total in a function everytime the function is called?
For example, I pass a file arg to a function. The function opens the
file and parses each record. I want to keep a running total of all the
records parsed no matter how many times the function is called.
.
|
|
|
| User: "Mike Wahler" |
|
| Title: Re: static extern help |
17 Jan 2005 02:50:24 PM |
|
|
"Joe" <jchristopher@comcast.net> wrote in message
news:1105993131.159121.116800@c13g2000cwb.googlegroups.com...
If I remove the "static" from the definition, how do I keep a running
total in a function everytime the function is called?
For example, I pass a file arg to a function. The function opens the
file and parses each record. I want to keep a running total of all the
records parsed no matter how many times the function is called.
// test.h (#include this file in any module where you
// need to refer to 'total' or 'func()'
extern int total;
void func(int);
// test.cpp
int total(0); /* initializer of zero not required, but it's my
preferred style to initialize everything */
void func(int i)
{
total += i;
}
// main.cpp
#include <iostream>
#include "test.h"
int main()
{
func(42);
func(99);
std::cout << "total is: " << total << '\n'; // prints 141
return 0;
}
But I'd try to eliminate the need for a global if at all
possible. However I cannot advise you about how to do
that without knowing more about what your code needs to
do, and your overall design.
-Mike
.
|
|
|
|
| User: "Victor Bazarov" |
|
| Title: Re: static extern help |
17 Jan 2005 02:35:56 PM |
|
|
Joe wrote:
If I remove the "static" from the definition, how do I keep a running
total in a function everytime the function is called?
I don't understand the question.
For example, I pass a file arg to a function. The function opens the
file and parses each record. I want to keep a running total of all the
records parsed no matter how many times the function is called.
So, keep it. What seems to be the problem?
.
|
|
|
|
|
| User: "Joe" |
|
| Title: Re: static extern help |
17 Jan 2005 03:02:21 PM |
|
|
If I remove the "static" from the definition, how do I keep a running
total in a function everytime the function is called?
For example, I pass a file arg to a function. The function opens the
file and parses each record. I want to keep a running total of all the
records parsed no matter how many times the function is called.
.
|
|
|
| User: "Mike Wahler" |
|
| Title: Re: static extern help |
17 Jan 2005 03:04:18 PM |
|
|
"Joe" <jchristopher@comcast.net> wrote in message
news:1105993146.597655.44530@f14g2000cwb.googlegroups.com...
If I remove the "static" from the definition, how do I keep a running
total in a function everytime the function is called?
Any object defined at file scope has static storage
duration by definition. You can give other files access
to it with an 'extern' declaration of it.
See my other post this thread for an example.
-Mike
.
|
|
|
|
|
|

|
Related Articles |
|
|