static extern help

J

Joe

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!
 
V

Victor Bazarov

Joe said:
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
 
J

Joe

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.
 
V

Victor Bazarov

Joe said:
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?
 
M

Mike Wahler

Joe said:
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
 
J

Joe

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.
 
M

Mike Wahler

Joe said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,197
Messages
2,571,041
Members
47,643
Latest member
ashutoshjha_1101

Latest Threads

Top