我有以下问题:我的(C++-)项目由多个子项目组成。在每个文件中,我都有几个文件,其中包含我想要在启动时运行的代码。到目前为止,我的解决方案是使用静态变量,它们在初始化时调用相应的代码,如下所示:
// Foo.cpp
static TFooRegistry sFooRegistry; // does stuff in constructor.
Run Code Online (Sandbox Code Playgroud)
当为每个子项目使用 dll 构建我的项目时,一切正常并且代码按预期运行。然而,当静态链接子项目时,链接器确定 Foo.o 不包含从外部引用的代码,并将其优化掉。当然,我可以在其他地方添加对 sFooRegistry 的引用,但这很乏味且容易出错。
有哪些(符合标准的)方法可以解决这个问题?
好的,我可以在 mac/gcc 和 win/visual studio 上做什么?
我有以下课程:
public abstract class A()
{
public static final SomeString = null;
static()
{
SomeString = "aaa";
}
}
Run Code Online (Sandbox Code Playgroud)
何时调用此静态方法以及如何调用?
创建这样的静态方法(没有名称/返回类型)的目的是什么?
我正在写一个日期类,我想要一个静态地图将"Jan"映射到1,依此类推.我想知道如何初始化静态地图.这就是我目前正在做的事情,但我只是觉得与Java中的静态块相比,额外的if语句是不优雅的.我理解C++程序的编译要复杂得多,但我仍然想知道是否存在更好的解决方案.
class date {
static map<string, int> month_map;
int month;
int year;
public:
class input_format_exception {};
date(const string&);
bool operator< (const date&) const;
string tostring() const;
};
map<string, int> date::month_map = map<string,int>();
date::date(const string& s) {
static bool first = true;
if (first) {
first = false;
month_map["Jan"] = 1;
month_map["Feb"] = 2;
month_map["Mar"] = 3;
month_map["Apr"] = 4;
month_map["May"] = 5;
month_map["Jun"] = 6;
month_map["Jul"] = 7;
month_map["Aug"] = 8;
month_map["Sep"] = 9;
month_map["Oct"] = 10;
month_map["Nov"] = 11; …Run Code Online (Sandbox Code Playgroud) 我经常这样做...
private void Check()
{
string s = "blah";
if ( new HashSet<string>{"Joe","Eddie","Buckethead"}.Contains(s) )
Debug.Log("Guitarist.");
}
Run Code Online (Sandbox Code Playgroud)
在管道中,HashSet实际上是否只创建一次(在启动时?编译时?)然后每次都使用?
顺便说一句,我假设如果你这样做:
private HashSet<string> g = new HashSet<string>()
{"Joe","Eddie","Buckethead"};
private void Check()
{
string s = "blah";
if ( g.Contains(s) )
Debug.Log("Guitarist.");
}
Run Code Online (Sandbox Code Playgroud)
那么确实,当然它只在类实例化时完成一次。(或者,也许在编译时/启动时?但无论如何,只有一次。)
如果我有一个从未使用的内部链接的全局,它的初始化程序是否可以保证运行?例如:
static int x = SideEffectfulFunction();
Run Code Online (Sandbox Code Playgroud)
要么
namespace {
int x = SideEffectfulFunction();
}
Run Code Online (Sandbox Code Playgroud)
为SideEffectfulFunction()保证被调用,即使x不会被引用?或者x删除是否合法?
我想知道使用如下构造是否可靠:
private static final Map<String, String> engMessages;
private static final Map<String, String> rusMessages;
static {
engMessages = new HashMap<String, String> () {{
put ("msgname", "value");
}};
rusMessages = new HashMap<String, String> () {{
put ("msgname", "????????");
}};
}
private static Map<String, String> msgSource;
static {
msgSource = engMessages;
}
public static String msg (String msgName) {
return msgSource.get (msgName);
}
Run Code Online (Sandbox Code Playgroud)
有可能我会得到NullPointerException因为msgSource初始化块之前会执行初始化块engMessages吗?
(关于为什么我不在msgSource初始化结束时进行初始化.块:只是味道问题;如果描述的结构不可靠,我会这样做)
我正在尝试使用实例化模型中的pertype实现跟踪方面.通过这种方式,我将能够为每种类型的每个类使用一个记录器.
从我们的一些例子中我可以找到这个代码来初始化记录器:
public abstract aspect TraceAspect pertypewithin(com.something.*) {
abstract pointcut traced();
after() : staticinitialization(*) {
logger = Logger.getLogger(getWithinTypeName());
}
before() : traced() {
logger.log(...);
}
//....
}
Run Code Online (Sandbox Code Playgroud)
遗憾的是,我无法将其完全转换为@AspectJ语法(这是我控制之外的项目要求),尤其是我需要设置记录器的部分,只执行一次该代码.
这可能吗?
谢谢,
我在使用这些静态成员初始化c ++类时遇到了麻烦.有关详细信息,请参阅我的代码
header.h
#ifndef HEADER_H
#define HEADER_H
#include <string>
using namespace std;
class Staff{ public: static string str;};
class Boss{ public: static string str;};
#endif
Run Code Online (Sandbox Code Playgroud)
staff.cpp
#include "header.h"
string Staff::str = "(staff)";
Run Code Online (Sandbox Code Playgroud)
boss.cpp
#include "header.h"
string Boss::str = "I call " + Staff::str;
Run Code Online (Sandbox Code Playgroud)
main.cpp中
#include <iostream>
#include "header.h"
int main(){cout << Boss::str << endl;}
Run Code Online (Sandbox Code Playgroud)
预编译:
g++ -c boss.cpp
g++ -c staff.cpp
ar rcs lib.a boss.o staff.o
ar rcs rlib.a staff.o boss.o
Run Code Online (Sandbox Code Playgroud)
编译,运行和结果:
g++ main.cpp staff.cpp boss.cpp ; ./a.out …Run Code Online (Sandbox Code Playgroud) c++ static-variables static-libraries segmentation-fault static-initialization
在Bruce Eckel的"Thinking in C++"的帮助下学习C++,坚持练习32,第10章.问题是如何更改链接顺序,Mirror :: test()调用对象m5返回false.这是我的代码.
#ifndef MIRROR_H_
#define MIRROR_H_
class Mirror {
public:
Mirror() {logic_ = true; self_ = 0;};
Mirror(Mirror *ptr) {self_ = ptr; logic_ = false;};
bool test() {
if (self_ != 0) {
return self_->test();
} else {
return logic_;
}
};
private:
bool logic_;
Mirror *self_;
};
#endif // MIRROR_H_
Run Code Online (Sandbox Code Playgroud)
任务
#include "mirror.h"
Mirror m1;
Run Code Online (Sandbox Code Playgroud)
#include "mirror.h"
extern Mirror m1;
Mirror m2 (&m1);
Run Code Online (Sandbox Code Playgroud)
#include "mirror.h"
extern Mirror m2;
Mirror m3 (&m2); …Run Code Online (Sandbox Code Playgroud) 如何确保静态字段的初始化仅在lambda的主体(或函数)中发生一次?
[] (string foo) {
static flat_hash_set<string> set;
// code to populate the set with some items.
// Question: how do I ensure this population code executed exactly once?
return set.contains(foo);
}
Run Code Online (Sandbox Code Playgroud)