【问题标题】:How should I implement a static collection of Strings in my class我应该如何在我的班级中实现字符串的静态集合
【发布时间】:2010-12-11 10:11:12
【问题描述】:

我对 C++ 很陌生,所以这可能是一个容易回答的问题。我正在编写一个类(Person),当创建一个 Person 时,应该从一组预定义的名称中为其分配一个随机名称。因此,在 Person 类中,我想定义某种可以随机访问的静态字符串集合,因此我还需要知道有多少。

我在这里也使用 Qt,所以解决方案最好使用标准库或 Qt 库中的东西。

我来自 Java 背景,在 Java 中我可能会做类似的事情:

private static final String[] NAMES = { "A", "B" };

在这种情况下,什么是等价的?

【问题讨论】:

    标签: c++ qt static constants


    【解决方案1】:

    你可以使用QStringList

    Person.h:

    class Person
    {
    private:
        static QStringList names;
    };
    

    Person.cpp:

    QStringList Person::names = QStringList() << "Arial" << "Helvetica" 
        << "Times" << "Courier";
    

    【讨论】:

      【解决方案2】:

      假设 C++03:

      class YourClass {
          static const char*const names[];
          static const size_t namesSize;
      };
      
      // in one of the translation units (*.cpp)
      const char*const YourClass::names[] = {"A", "B"};
      const size_t YourClass::namesSize = sizeof(YourClass::names) / sizeof(YourClass::names[0]);
      

      假设 C++0x:

      class YourClass {
          static const std::vector<const char*> names;
      };
      
      // in one of the translation units (*.cpp)
      const vector<const char*> YourClass::names = {"A", "B"};
      

      当然你也可以使用你最喜欢的字符串类型来代替const char*

      【讨论】:

      • 感谢您的回答,考虑到它们都很好,很难选择一个可以接受的答案,但我只是选择了 QStringList 答案,纯粹是因为它非常符合我正在尝试的事实学习 Qt。
      • @Dave:虽然我能理解你,但我仍然更喜欢尽可能编写更标准和可移植的代码。即使它不是那么整洁(C++03 示例)。
      【解决方案3】:

      首先,一个非常简单的程序,用于从静态数组生成随机名称。可以在下面找到正确的类实现。

      #include <iostream>
      #include <string>
      #include <stdlib.h>
      #include <time.h>
      
      // import the std namespace (to avoid having to use std:: everywhere)
      using namespace std;
      // create a constant array of strings
      static string const names[] = { "James", "Morrison",
                                      "Weatherby", "George", "Dupree" };
      // determine the number of names in the array
      static int const num_names = sizeof(names)/sizeof(names[0]);
      
      // declare the getRandomName() function
      string getRandomName();
      
      // standard main function
      int main (int argc, char * argv[])
      {
          // seed the random number generator
          srand(time(0));
          // pick a random name and print it
          cout << getRandomName() << endl;
          // return 0 (no error)
          return 0;
      }
      
      // define the getRandomName() function
      string getRandomName()
      {
          // pick a random name (% is the modulo operator)
          return names[rand()%num_names];
      }
      

      类实现

      Person.h

      #ifndef PERSON_
      #define PERSON_
      
      #include <string>
      
      class Person
      {
          private:
              std::string p_name;
          public:
              Person();
              std::string name();
      };
      
      #endif
      

      Person.cpp

      #include "Person.h"
      #include <stdlib.h>
      
      using namespace std;
      
      static string const names[] = { "James", "Morrison",
                                      "Weatherby", "George", "Dupree" };
      static int const num_names = sizeof(names)/sizeof(names[0]);
      
      Person::Person() : p_name(names[rand()%num_names]) { }
      string Person::name() { return p_name; }
      

      ma​​in.cpp

      #include <iostream>
      #include <string>
      #include <stdlib.h>
      #include <time.h>
      #include "Person.h"
      
      using namespace std;
      
      int main (int argc, char * argv[])
      {
          // seed the random number generator
          srand(time(0));
      
          // create 3 Person instances
          Person p1, p2, p3;
      
          // print their names
          cout << p1.name() << endl;
          cout << p2.name() << endl;
          cout << p3.name() << endl;
      
          // return 0 (no error)
          return 0;
      }
      

      【讨论】:

      • 一个很好的解释,但using namespace std; 是个坏主意,可能会导致一大堆问题,其中许多问题由于命名空间冲突已在 SO 上发布。输入std:: 几乎没有时间。
      • 在头文件中使用“using namespace std;”是错误的。在源文件中使用它是非常好的。
      • 虽然:) stackoverflow.com/questions/1452721/… 上的争论自然而然地爆发了
      • 感谢您加倍努力将其全部制作成完整的样本。
      • @DaveJohnston:我很高兴。我知道当你刚开始时很难理解这一切:)
      猜你喜欢
      • 2023-03-09
      • 2015-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多