[英]C++: How to use extern variables in a struct
我嘗試遵循ETSI規范來實現一個軟件。 現在,我必須編寫一個結構,其中將一個變量聲明為extern。
file2.cpp
struct struct_one {
extern Algo algo;
int x;
}
file1.cpp
struct struct_two {
Algo algo;
char c;
}
那么,如何“告訴”編譯器兩個“ algo”變量包含相同的內容? 在file2中包含file1是否足夠?
或者我該怎么辦?
問候
您無法做到這一點,但是您可以通過以下方式做到這一點:
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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.