简体   繁体   English

使用在类外部声明的数据类型

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

相关问题 在类或外部声明的typedef结构? - typedef structs declared inside class or outside? 在 class 内部声明并在 class 外部定义的访问器不起作用 - Accessor declared inside the class, and defined outside the class doesn't work 在类访问混乱中声明的枚举类型 - enum type declared inside a class access confusion 当我使用已在类外声明并稍后在类内声明的名称时,这是未定义的行为还是非法行为? - Is it undefined behavior or illegal, when I use a name that is both already declared outside of the class and later declared inside the class? 对在类外声明的函数的未定义引用 - Undefined reference to function declared outside the class 在类模板中找不到声明的类型 - Declared type not found in class template class 内部还是外部声明? - Declaration inside class or outside? 如何将在一个类中声明的类的数据类型访问到另一个类中(两者都在不同的翻译单元中)? - How to access data type of a class declared in one class into another class (both in different translation unit)? 在类内部声明模板化函数(在容器类型之上),并在容器类型的模板类外部对其进行定义- - declaration of templated function (over container type) inside class and definition of it outside a template class over container type- 课堂内外,为什么相同的数据有不同的值? - Inside and outside the class, why the same data has different values?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM