简体   繁体   中英

How to restrict access to static variables in C++?

I have a C-function called "count" that looks like this:

void count(){ 
  static int c = 0;
  printf("Counter=%i", c);
  c++;
}

Futhermore I have a vector of Cpp-objects and each object calls the "count" function. Since the counter variable is static a call made by one object will increase the counter value for all other objects as well. What I actually want is a dedicated counter for each object given the restrication that the "count"-function is Device-Under-Test and may not be changed. I think this should be possible using namespaces... Any ideas?


My initial idea was to use namespaces ...

namespace c1 {
#ifdef __cplusplus
  extern "C" {
#endif
    #include "count.h"
#ifdef __cplusplus
  }
#endif
}

namespace c2 {
#ifdef __cplusplus
  extern "C" {
#endif
    #include "count.h"
#ifdef __cplusplus
  }
#endif
}

And call from within Cpp-Object like this ...

if (objNr == 1) c1::count();
else if (objNr == 2) c2::count();
...

It did not work for me. Any idea why?

The problem as asked can not be solved. If the function is unmodifiable, there is nothing which can be done to start counting individual instances.

Variables with static storage class , such as the one in your example, are global in the sense that there is only one copy anywhere in the program. This is independent of of their linkage , which determines from where they can be referenced. Regardless of their storage class, functions' local variables have no linkage, meaning that they can be directly accessed only from within the function body.

If you cannot modify the function, then there is no way for it to make variable c accessible elsewhere (such as by exposing a pointer to it), so there is no alternative for the test routines to, say, reset its value between tests or to read it out. Therefore, if different test objects must have their own copies of that particular variable, then it follows that they must have their own copy of the function that contains it.

The easiest and most general way to achieve that is to run each test object in a separate program. It might also be possible to play games such as dynamically loading and unloading a library containing that function (per @VadimKey), but that depends on features outside those of standard C or C++, and it makes the test environment rather different from most of the other environments the function is likely to see.

Otherwise, if multiple objects must run tests within the same run of the same test program, then there is no way to make them have private copies of functions' static variables. Your best bet might simply be to structure your tests to accommodate that.

If you can access to source code change it in some way to make this counter external. Either pass it as parameter, either make a class with counter as a member.

If you can't change the source code with this function then you may create a wrapper class with a separate counter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM