[英]How can I use struct defined in one class in a different class in C++?
I have this struct in a class, Class1
.: 我在
Class1
类中有此结构:
static struct count {
int member1;
int member2;
int member3;
} count1, count2, count3;
In Class1
, I will be incrementing all three of the count
variables and using it again in Class2
in a separate file. 在
Class1
,我将增加所有三个count
变量,并在Class2
中的单独文件中再次使用它。 How can I do this? 我怎样才能做到这一点? Do I need to include
Class1
in a header file? 我是否需要在头文件中包含
Class1
?
the static variables scope is limited but lifetime is through out the program, if you have declare the count structure inside the class1 definition then you cant access this static count object in Class2 even though you include the file where Class2 has been defined. 静态变量的作用域是有限的,但是整个程序的生命周期是整个程序,如果您在class1定义中声明了计数结构,则即使包含了定义了Class2的文件,也无法在Class2中访问此静态计数对象。 You have to make count structure global in the file and then can include the file to access it in Class2
您必须在文件中全局设置计数结构,然后可以包含该文件以在Class2中对其进行访问
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.