C++ 静态类成员定义内初始化的最佳替代方案?(用于SVN关键字)
我将.cpp文件的扩展SVN关键字文本存储在“static char const*const”类成员中,并希望尽可能类似地存储.h描述。简言之,我需要保证静态成员(可能在.cpp文件中)的单个实例化是一个自动生成的非整数文本,存在于一个潜在的共享.h文件中。不幸的是,该语言没有尝试解析由类定义之外的赋值产生的多个实例化,并且明确禁止类定义内部的非整数初始化。我最好的尝试(使用静态包装内部类)不是太脏,但我真的想做得更好。有没有人有办法在下面创建包装器的模板,或者有一个完全优越的方法C++ 静态类成员定义内初始化的最佳替代方案?(用于SVN关键字),c++,header,initialization,static-members,C++,Header,Initialization,Static Members,我将.cpp文件的扩展SVN关键字文本存储在“static char const*const”类成员中,并希望尽可能类似地存储.h描述。简言之,我需要保证静态成员(可能在.cpp文件中)的单个实例化是一个自动生成的非整数文本,存在于一个潜在的共享.h文件中。不幸的是,该语言没有尝试解析由类定义之外的赋值产生的多个实例化,并且明确禁止类定义内部的非整数初始化。我最好的尝试(使用静态包装内部类)不是太脏,但我真的想做得更好。有没有人有办法在下面创建包装器的模板,或者有一个完全优越的方法 // Foo
// Foo.h: class with .h/.cpp SVN info stored and logged statically
class Foo {
static Logger const verLog;
struct hInfoWrap;
public:
static hInfoWrap const hInfo;
static char const *const cInfo;
};
// Would like to eliminate this per-class boilerplate.
struct Foo::hInfoWrap {
hInfoWrapper() : text("$Id$") { }
char const *const text;
};
//Helper.h:构造时输出,没有后续活动或存储字段
类记录器{
记录器(字符常量*信息1,字符常量*信息2){
也许你可以用宏做点什么
// Foo.h
#define FOO_HINFO "$Id$"
我通常避免使用宏,但在这种情况下,它可能是最干净的解决方案。可能是静态函数
// Foo.h:
class Foo {
static Logger const verLog;
static char const*const getHInfo() { return "$Id$"; }
public:
static char const *const cInfo;
};
// Foo.cpp: static inits called here
char const *const Foo::cInfo = "$Id$";
Logger const Foo::verLog(Foo::cInfo, Foo::getHInfo());
我完全忘记了类内定义静态方法定义是合法的。这就是我最终要使用的。谢谢!因为我试图找到一个解决方案,我的同龄人也希望使用它,如果不是因为Alsk让我意识到我有更大的疏忽。不过非常感谢您的回复!
// Foo.h
#define FOO_HINFO "$Id$"
// Foo.cpp
char const *const Foo::hInfo = FOO_HINFO;
// Foo.h:
class Foo {
static Logger const verLog;
static char const*const getHInfo() { return "$Id$"; }
public:
static char const *const cInfo;
};
// Foo.cpp: static inits called here
char const *const Foo::cInfo = "$Id$";
Logger const Foo::verLog(Foo::cInfo, Foo::getHInfo());