[英]using a data type declared inside a class outside
I am new to c++ so here is verry silly question for few of you. 我是C ++的新手,所以对你们中的几个人来说这是一个愚蠢的问题。
class DList {
public:
struct DNode {
int data;
DNode* next;
DNode* prev;
DNode(DNode* ptr1, DNode* ptr2, int val)
{
next = ptr1;
prev = ptr2;
data = val;
}
~DNode() {}
public:
DNode* getNext() {return next;}
int getNodeVal() {return data;}
};
This is the DList structure for me.suppose i want to use datatype DNode outside this class in some other cpp file to declare data of DNode type.how can i use it. 这是我的DList结构。假设我想在其他cpp文件中的此类之外使用数据类型DNode声明DNode类型的数据,我该如何使用它。
It's just a matter of name qualification: 这只是名称限定的问题:
DList::Dnode x;
This also works for externally referring to any variables or functions declared statically within the class. 这也适用于从外部引用类中静态声明的任何变量或函数。
class DList {
public:
struct DNode {
int data;
DNode* next;
DNode* prev;
DNode(DNode* ptr1, DNode* ptr2, int val)
{
next = ptr1;
prev = ptr2;
data = val;
}
~DNode() {}
public:
DNode* getNext() {return next;}
int getNodeVal() {return data;}
};
static int counter;
static int f() {/**/} //do some stateless operation related to the class
};
//...
DList::counter++;
int result = DList::f();
You can declare a variable of that type with: 您可以使用以下方法声明该类型的变量:
DList::DNode myNode;
If you are in some other cpp file, make sure you #include "DList.h"
or whatever the name of that (hopefully) header file is. 如果您在其他cpp文件中,请确保
#include "DList.h"
或该(希望的)头文件的名称。 If it isn't a header file, you should move it to one and possibly consider moving the implementation details to a .cpp file. 如果它不是头文件,则应将其移动到一个头文件,并可能考虑将实现详细信息移动到.cpp文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.