我尝试按照 ETSI 规范实现一款软件。 现在我必须编写一个结构,其中一个变量被声明为 extern。
file2.cpp
struct struct_one {
extern Algo algo;
int x;
}
file1.cpp
struct struct_two {
Algo algo;
char c;
}
那么,我如何“告诉”编译器这两个“算法”变量包含相同的东西? 将 file1 包含在 file2 中是否足够?
或者我必须做什么?
问候
最佳答案
你不能那样做,但你可以这样做:
file1.cpp
struct struct_two {
Algo algo;
char c;
}
file2.cpp
struct struct_one {
explicit struct_one(struct_two& t) : algo(t.algo) {}
Algo& algo;
int x;
}
或使用组合模式('有'关系):
file1.cpp
struct struct_two {
Algo algo;
char c;
}
file2.cpp
struct struct_one {
struct_two t;
int x;
}
关于C++:如何在结构中使用外部变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27124139/