简体   繁体   English

如何在C ++中限制对静态变量的访问?

[英]How to restrict access to static variables in C++?

I have a C-function called "count" that looks like this: 我有一个名为“ count”的C函数,如下所示:

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. 此外,我有一个Cpp对象向量,每个对象都称为“计数”函数。 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. 我实际想要的是给每个对象一个专用的计数器,因为“ count”功能是“ Device-Under-Test”,并且可能不会更改。 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 ... 然后像这样从Cpp-Object内调用...

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. 具有static 存储类的变量(例如您的示例中的变量)是全局的,这意味着程序中的任何位置都只有一个副本。 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. 如果您不能修改该函数,则它无法使变量c在其他位置可访问(例如,通过公开指向它的指针),因此测试例程没有其他选择,例如,在测试之间或在测试之间重置其值。读出来。 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. 也可以玩游戏,例如动态加载和卸载包含该函数的库(每个@VadimKey),但这取决于标准C或C ++之外的功能,这使测试环境与大多数该功能可能会在其他环境中看到。

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. 要么将其作为参数传递,要么创建一个以counter为成员的类。

If you can't change the source code with this function then you may create a wrapper class with a separate counter. 如果您无法使用此函数更改源代码,则可以创建带有单独计数器的包装器类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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