C++ 静态类成员的正确使用
拥有静态类成员的正确方式是什么 我正在尝试创建一个metrics类,以便在所有文件中包含“metrics.h”,并使用其中的某些变量(静态变量)来跟踪多个单独编译的库中的计时信息 其中一个库对其他库都是通用的,因此当我使用metrics.h编译它时,它可以很好地编译,但是当我尝试编译使用common的其他库时,会出现“多定义”错误,以及一些未定义的引用 这个通用度量类应该是什么样子?我不想实例化它来使用变量,我只想使用 指标::开始时间= 和度量::calcTime= 在公共库中,或在头文件中链接到公共库的其他库中:C++ 静态类成员的正确使用,c++,static,linker,C++,Static,Linker,拥有静态类成员的正确方式是什么 我正在尝试创建一个metrics类,以便在所有文件中包含“metrics.h”,并使用其中的某些变量(静态变量)来跟踪多个单独编译的库中的计时信息 其中一个库对其他库都是通用的,因此当我使用metrics.h编译它时,它可以很好地编译,但是当我尝试编译使用common的其他库时,会出现“多定义”错误,以及一些未定义的引用 这个通用度量类应该是什么样子?我不想实例化它来使用变量,我只想使用 指标::开始时间= 和度量::calcTime= 在公共库中,或在头文件中链
class Metrics {
public:
static int startTime ;
} ;
仅在一个cpp
文件中:
int Metrics::startTime ;
在头文件中:
class Metrics {
public:
static int startTime ;
} ;
仅在一个cpp
文件中:
int Metrics::startTime ;
除此之外,还需要正确实现构造函数
除此之外,所有构造函数都是正确的实现您不应该以这样的方式组合库,即相同的代码会多次链接到程序中。在一个库中隔离度量类,以及它应该打包的任何其他类(请参阅发布的六个打包原则以获取指导)。将依赖它的其他类放在其他库中(或者如果这些打包原则表明它们属于同一个库,则将它们全部放在一个库中)。链接程序时,请链接所需的所有库,包括依赖于度量类的库和包含度量类的(一个)库。组合库时,不应使同一代码多次链接到程序中。在一个库中隔离度量类,以及它应该打包的任何其他类(请参阅发布的六个打包原则以获取指导)。将依赖它的其他类放在其他库中(或者如果这些打包原则表明它们属于同一个库,则将它们全部放在一个库中)。链接程序时,请链接所需的所有库,包括依赖于度量类的库和包含度量类的(一)个库。与其定义多个静态成员,不如将它们全部打包到一个静态
结构
成员中
比直接实例化和访问这个静态成员更好的是将它隐藏在getter函数中
class metrics {
...
struct measurements { // struct may be private; you can still access
double startTime; // the members of the result of the getter fn.
double calcTime;
...
};
public:
static measurements &getMeasurements() {
static measurements instance;
return instance;
}
};
这样,您就不需要向
.cpp
文件添加任何内容。第一次调用getMeasurements
时创建measurements
对象实例
,并为所有后续调用返回相同的对象。与其定义许多静态成员,不如将它们打包到一个静态struct
成员中
比直接实例化和访问这个静态成员更好的是将它隐藏在getter函数中
class metrics {
...
struct measurements { // struct may be private; you can still access
double startTime; // the members of the result of the getter fn.
double calcTime;
...
};
public:
static measurements &getMeasurements() {
static measurements instance;
return instance;
}
};
这样,您就不需要向
.cpp
文件添加任何内容。第一次调用getMeasurements
时,将创建measurements
对象实例
,并为所有后续调用返回相同的对象。Metrics.h
#ifndef _METRICS_H
#define _METRICS_H
class Metrics {
...
public:
Metrics();
//declare the static variables in the header for the class
static int startTime;
static int calcTime;
}
#endif
Metrics.cpp
#include "Metrics.h"
Metrics::Metrics(){
...
}
//define and initialize the variables in the implementation of the class
int Metrics::startTime = 15;
int Metrics::calcTime = 20;
#include "Metrics.h"
int main(){
//use the static variables elsewhere
int a = Metrics::startTime;
}
main.cpp
#include "Metrics.h"
Metrics::Metrics(){
...
}
//define and initialize the variables in the implementation of the class
int Metrics::startTime = 15;
int Metrics::calcTime = 20;
#include "Metrics.h"
int main(){
//use the static variables elsewhere
int a = Metrics::startTime;
}
Metrics.h
#ifndef _METRICS_H
#define _METRICS_H
class Metrics {
...
public:
Metrics();
//declare the static variables in the header for the class
static int startTime;
static int calcTime;
}
#endif
Metrics.cpp
#include "Metrics.h"
Metrics::Metrics(){
...
}
//define and initialize the variables in the implementation of the class
int Metrics::startTime = 15;
int Metrics::calcTime = 20;
#include "Metrics.h"
int main(){
//use the static variables elsewhere
int a = Metrics::startTime;
}
main.cpp
#include "Metrics.h"
Metrics::Metrics(){
...
}
//define and initialize the variables in the implementation of the class
int Metrics::startTime = 15;
int Metrics::calcTime = 20;
#include "Metrics.h"
int main(){
//use the static variables elsewhere
int a = Metrics::startTime;
}
我可以在多个cpp文件中使用Metrics::startTime吗?可以,但是声明(
int Metrics::startTime;
)必须正好出现在一个文件中。我可以在多个cpp文件中使用Metrics::startTime吗?可以,但是声明(int Metrics::startTime;
)必须发生在一个文件中。构造函数与此有什么关系?当功能/资源可能有限/不可用时,您必须在程序启动时初始化一次潜在的复杂类型。因此,您必须保证正确的初始化。还有其他方法可以声明共享数据。。。但他们每个人都有问题。最好避免使用静态数据,并尽可能晚地推迟其需求,或将其作为另一个实例的依赖项(变量)。构造函数与此有何关系?当功能/资源可能有限/不可用时,您必须在程序启动时初始化一次潜在的复杂类型。因此,您必须保证正确的初始化。还有其他方法可以声明共享数据。。。但他们每个人都有问题。最好避免使用静态数据,并尽可能延迟其要求,或将其作为另一个实例的依赖项(变量)。我同意-->不使用全局变量:)我同意-->不使用全局变量:)