【问题标题】:How can I initialize char arrays in a constructor?如何在构造函数中初始化 char 数组?
【发布时间】:2012-05-12 10:38:12
【问题描述】:

我在声明和初始化 char 数组时遇到问题。它总是显示随机字符。我创建了一小段代码来展示我在更大的程序中尝试的内容:

class test
{
    private:
        char name[40];
        int x;
    public:
        test();
        void display()
        {
            std::cout<<name<<std::endl;
            std::cin>>x;
        }
};
test::test()
{
    char name [] = "Standard";
}

int main()
{   test *test1 = new test;
    test1->display();
}

很抱歉,如果我的格式不好,我几乎无法弄清楚这个网站,更不用说如何修复我的代码了 :(

【问题讨论】:

  • 好的,所以我现在使用 std::string,但是在更大的程序中,这是因为我使用了这样的 get 函数: void get(char prompt[], int size,char b[]) 其中 char b[] 是我之前的 char 数组。我将如何修改它以便将字符串传递给我的 get 函数?
  • 返回 std::string 对象而不是 void

标签: c++ arrays char


【解决方案1】:

实际上提供了两种方法。您可以在其声明行中默认该成员,也可以使用构造函数初始化列表。

声明行初始化示例:

class test1 {
    char name[40] = "Standard";
public:
    void display() { cout << name << endl; }
};

构造函数初始化示例:

class test2 {
    char name[40];
public:
    test2() : name("Standard") {};
    void display() { cout << name << endl; }
};

您可以在此处查看这两个示例:http://ideone.com/zC8We9

我个人的偏好是使用声明行初始化,因为:

  1. 如果不需要构造其他变量,则允许使用生成的默认构造函数
  2. 在需要多个构造函数的情况下,这允许变量仅在一个位置进行初始化,而不是在所有构造函数初始化列表中进行初始化

说了这么多,使用char[] 作为生成的默认赋值运算符可能会被认为是有害的,并且复制/移动构造函数将不起作用。这可以通过以下方式解决:

  1. 制作会员const
  2. 使用char*(如果成员只保存文字字符串,这将不起作用)
  3. 一般情况下应该首选std::string

【讨论】:

    【解决方案2】:

    如果没有特殊原因不使用std::string,请使用std::string

    但是如果你真的需要初始化那个字符数组成员,那么:

    #include <assert.h>
    #include <iostream>
    #include <string.h>
    using namespace std;
    
    class test
    {
        private:
            char name[40];
            int x;
        public:
            test();
            void display() const
            {
                std::cout<<name<<std::endl;
            }
    };
    
    test::test()
    {
        static char const nameData[] = "Standard";
    
        assert( strlen( nameData ) < sizeof( name ) );
        strcpy( name, nameData );
    }
    
    int main()
    {
        test().display();
    }
    

    【讨论】:

    • 我认为 main 正文有点神秘,衡量 OP 的学习阶段:)
    • 所以我决定使用std::string,但是根据我上面的评论,我如何将字符串传递给另一个函数?
    【解决方案3】:

    您的构造函数没有设置成员变量name,而是声明了一个局部变量。一旦局部变量在构造函数结束时超出范围,它就会消失。同时成员变量还没有初始化,被随机垃圾填满。

    如果您要使用老式字符数组,您还需要使用像strcpy 这样的老式函数来复制到成员变量中。如果您只想将其设置为空字符串,您可以使用name[0] = 0 对其进行初始化。

    【讨论】:

      【解决方案4】:

      考虑到您将问题标记为 C++,您应该使用 std::string:

      #include <string>
      
      class test
      {
          private:
              std::string name;
              int x;
          public:
              test();
              void display()
              {
                  std::cout<<name<<std::endl;
                  std::cin>>x;
              }
      };
      test::test() : name("Standard")
      {
      
      }
      

      【讨论】:

      • 非常感谢这对您有很大帮助。
      • 我不太明白稍后发布的“相同”答案如何获得更多支持。我的回答可以说是(相当多)从一开始就更完整......
      • 我还没有真正得到这个网站,我试图对两者都投赞成票,但不能。对不起。
      • @user1371155 没关系,我实际上并没有责怪任何人:)
      • @sehe:为了不偏袒第一个答案,并希望给其他答案更多的可见性,具有相同分数的答案是随机排序的。这意味着,不是第一个发布获得更多知名度的人,而是第一个获得支持的人;这可以说是更好,但不是很多。
      【解决方案5】:

      由于您使用的是 C++,我建议使用字符串而不是 char 数组。否则,您需要使用 strcpy(或朋友)。

      另外,您忘记删除 test1 实例。

      #include <iostream>
      #include <string>
      
      class test
      {
          private:
              std::string name;
              int x;
          public:
              test();
              void display()
              {
                  std::cout<<name<<std::endl;
              }
      };
      
      test::test()
      {
          name = "Standard";
      }
      
      int main()
      {   
          test test1;
          test1.display();
      
          std::cin>>x;
      }
      

      【讨论】:

      • 非常感谢你们很棒!
      • char 数组的用例:C++ 在 std lib 不存在的地方可用,例如微控制器。
      猜你喜欢
      • 2020-10-04
      • 2012-10-23
      • 2010-09-19
      • 2017-08-15
      • 2023-04-10
      • 1970-01-01
      • 2011-05-02
      • 1970-01-01
      • 2014-10-04
      相关资源
      最近更新 更多