473,320 Members | 1,817 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,320 software developers and data experts.

static extern help

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!

Jul 22 '05 #1
6 1290
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
Jul 22 '05 #2
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.

Jul 22 '05 #3
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?
Jul 22 '05 #4
"Joe" <jc**********@comcast.net> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.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
Jul 22 '05 #5
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.

Jul 22 '05 #6

"Joe" <jc**********@comcast.net> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.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
Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: ik | last post by:
Hello all, Under C++ when 'extern' is used with out the string literal "C" does it act the same as a 'static' ? When is it appropriate using 'extern' ? Thanks ~Ik
9
by: Bryan Parkoff | last post by:
I have noticed that C programmers put static keyword beside global variable and global functions in C source codes. I believe that it is not necessary and it is not the practice in C++. Static...
10
by: Mark A. Gibbs | last post by:
I have a question about mixing C and C++. In a C++ translation unit, I want to define a function with internal linkage and C calling convention. Here's a sample of what I want to do: //...
4
by: Sean | last post by:
I am a little confused by the "extern inline and static inline" rules. I understand that "extern inline" guarantees no function storage is created (all are inlined). But the following test seems to...
4
by: nospam_timur | last post by:
Let's say I have two files, myfile.h and myfile.c: myfile.h: int myfunction(int x); myfile.c: #include "myfile.h"
5
by: kselvaakumar | last post by:
I have declared a global structure like this: struct { float a; float b; }static one; This is in a separate header file file1.h; Now how can i initialise those variables one.a and one.b in...
32
by: lcdgoncalves | last post by:
Hi everyone Is there a real need to use keyword static with functions, if we simply don't declare their prototypes in .h file? Many textbooks avoid to discuss this matter and/or discuss only...
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...
5
by: Michael Oswald | last post by:
Hello all, I'm working on a project where I came across some situations, where the GUI library works with normal C callbacks. The code has been implemented by many people, so I came across...
28
by: Why Tea | last post by:
I seem to remember that in ANSI C, all static functions should have their function prototypes listed at the beginning of the file for better consistency (or something like that). I googled and...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.