【问题标题】:Define a struct inside a class in C++在 C++ 中的类中定义结构
【发布时间】:2011-02-02 08:19:13
【问题描述】:

谁能给我一个例子,说明如何在 C++ 中的 class 中定义一种新类型的 struct

谢谢。

【问题讨论】:

  • 另请注意,最受好评的答案的相同技术可用于在类中定义类,在结构中定义结构,在结构中定义类。 class 和 struct 仅在其成员的默认可见性(分别为私有和公共)方面有所不同。
  • ...以及它们的默认继承类型(分别为私有和公有)。

标签: c++ class struct


【解决方案1】:

类似这样的:

class Class {
    // visibility will default to private unless you specify it
    struct Struct {
        //specify members here;
    };
};

【讨论】:

    【解决方案2】:

    可能在某些头文件中声明类和嵌套结构

    class C {
        // struct will be private without `public:` keyword
        struct S {
            // members will be public without `private:` keyword
            int sa;
            void func();
        };
        void func(S s);
    };
    

    如果你想分离实现/定义,可能在一些 CPP 文件中

    void C::func(S s) {
        // implementation here
    }
    void C::S::func() { // <= note that you need the `full path` to the function
        // implementation here
    }
    

    如果你想内联实现,其他答案就可以了。

    【讨论】:

    • 所以我有一个内部包含三个变量的结构,并且我能够使用内部的 'private:' 关键字编译我的代码。那么该结构“私有”的界限是什么?定义结构的类可以访问这些变量中的任何一个吗?我是否绝对需要公共函数指针来引用主类中的函数?基本上我所拥有的是一个由结构表示的测试用例向量。
    • “完整路径”刚刚救了我的命!
    • 只要你在炫耀在类体之外定义成员函数的想法,也许你可以编辑这个答案来展示如何在类体之外定义结构本身?这在定义嵌套类(例如迭代器类型)的情况下更为常见,但可能是相关的。
    • @templatetypedef 我不明白你的意思..也许你可以添加另一个答案或编辑现有答案来说明它。
    【解决方案3】:

    这里的其他答案已经演示了如何在类中定义结构。还有另一种方法可以做到这一点,那就是在类内部声明结构,但在外部定义它。这可能很有用,例如,如果结构相当复杂并且可能以一种可以从其他地方详细描述中受益的方式独立使用。

    其语法如下:

    class Container {
    
        ...
    
        struct Inner; // Declare, but not define, the struct.
    
        ...
    
    };
    
    struct Container::Inner {
       /* Define the struct here. */
    };
    

    您通常会在定义嵌套类而不是结构的上下文中看到这一点(一个常见的例子是为集合类定义迭代器类型),但我认为为了完整性,值得在这里炫耀一下。

    【讨论】:

      【解决方案4】:

      类似:

      class Tree {
      
       struct node {
         int data;
         node *llink;
         node *rlink;
       };
       .....
       .....
       .....
      };
      

      【讨论】:

      • 我正在尝试做类似的事情(创建)霍夫曼树。我怀疑您将如何创建节点对象以及如何使其在主函数中可访问?
      【解决方案5】:

      是的,你可以。在 C++ 中,类和结构有点相似。我们不仅可以在一个类中定义结构,还可以在一个类中定义一个类。它被称为内部类。

      作为一个例子,我正在添加一个简单的 Trie 类。

      class Trie {
      private:
          struct node{
              node* alp[26];
              bool isend;
          };
          node* root;
          node* createNode(){
              node* newnode=new node();
              for(int i=0; i<26; i++){
                  newnode->alp[i]=nullptr;
              }
              newnode->isend=false;
              return newnode;
          }
      public:
          /** Initialize your data structure here. */
          Trie() {
              root=createNode();
          }
      
          /** Inserts a word into the trie. */
          void insert(string word) {
              node* head=root;
              for(int i=0; i<word.length(); i++){
                  if(head->alp[int(word[i]-'a')]==nullptr){
                      node* newnode=createNode();
                      head->alp[int(word[i]-'a')]=newnode;
                  }
                  head=head->alp[int(word[i]-'a')];
              }
              head->isend=true;
          }
      
          /** Returns if the word is in the trie. */
          bool search(string word) {
              node* head=root;
              for(int i=0; i<word.length(); i++){
                  if(head->alp[int(word[i]-'a')]==nullptr){
                      return false;
                  }
                  head=head->alp[int(word[i]-'a')];
              }
              if(head->isend){return true;}
              return false;
          }
      
          /** Returns if there is any word in the trie that starts with the given prefix. */
          bool startsWith(string prefix) {
              node* head=root;
              for(int i=0; i<prefix.length(); i++){
                  if(head->alp[int(prefix[i]-'a')]==nullptr){
                      return false;
                  }
                  head=head->alp[int(prefix[i]-'a')];
              }
              return true;
          }
      };
      
      /**
       * Your Trie object will be instantiated and called as such:
       * Trie* obj = new Trie();
       * obj->insert(word);
       * bool param_2 = obj->search(word);
       * bool param_3 = obj->startsWith(prefix);
       */
      

      【讨论】:

        【解决方案6】:
        #include<iostream>
        using namespace std;
        
        class A
        {
            public:
                struct Assign
                {
                    public:
                        int a=10;
                        float b=20.5;
                    private:
                        double c=30.0;
                        long int d=40;
                 };
                 struct Assign ALT;
        };
        
        class B: public A
        {
        public:
            int x = 10;
        private:
            float y = 20.8;
        };
        
        int main()
        {
           B myobj;
           A obj;
           //cout<<myobj.a<<endl;
           //cout<<myobj.b<<endl;
           //cout<<obj.a<<endl;
           //cout<<obj.b<<endl;
           cout<<myobj.ALT.a<<endl;
        
            return 0;
        }
        
            enter code here
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-10-20
          • 2018-06-19
          • 1970-01-01
          • 1970-01-01
          • 2020-11-27
          • 1970-01-01
          • 2020-11-14
          相关资源
          最近更新 更多