简体   繁体   English

如何在C ++中的不同类中使用在一个类中定义的结构?

[英]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.

相关问题 使用类/结构的不同方法 - C++ - Different methods to use a class/struct - C++ 如何在C ++中为不同的模板类型使用不同的类定义? (类重载?) - How can I use different definition of class for different template type in C++? (Class Overloading?) 如何在类C ++中使用模板化结构 - How to use templated struct in class c++ 如果未在类C ++中定义方法,如何在类对象上使用方法 - How to use a method on a class object if method not defined in class c++ 我可以定义包含结构的类吗?结构里面有这个类吗 - Can I defined the class that contain the struct and the struct have this class inside C ++:如何对同一数据类使用不同的方法实现? - C++: How can I use different implementation of methods with the same data class? 如何在C ++的类体中使用类def中定义的字符数组? - How do I use an array of character arrays defined in the class def in the class body in C++? 如何将C ++类/结构转换为原始/不同类型/类/结构? - How to convert C++ class/struct to a primitive/different type/class/struct? 我如何指向另一个类中定义的结构? - How can I point to a struct that is defined in another class? 我可以在 c++ 中键入未命名的结构/类吗? - Can I typedef unnamed struct/class in c++?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM