简体   繁体   English

如何在成员初始值设定项列表中初始化数组

[英]How to initialize an array in the member initializer list

Complete Beginner in C++. 使用C ++的完整初学者。

This is a member initialization list: 这是成员初始化列表:

Student.cpp Student.cpp

Student::Student(int studentID ,char studentName[40]) : id(studentID), name(studentName){};

Student.h Student.h

class Student{

protected:
    char name[40];
    int id;
}

My problem is that name is of type char[40] , so, name(studentName) displays an error of: 我的问题是name的类型为char[40] ,因此name(studentName)显示以下错误:

a value of type "char *" cannot be used to initialize an entity of type "char [40]"

How can I initialize name array, to studentName array in the member initializer list? 如何在成员初始值设定项列表中将name数组初始化为studentName数组? I don't want to use string, and I have tried strcpy and didn't work 我不想使用字符串,但是我尝试了strcpy却没有用

Since you can't initialize (raw) arrays with other arrays or even assign arrays in C++, you basically have two possibilities: 由于您无法使用其他数组初始化(原始)数组,甚至无法在C ++中分配数组,因此您基本上有两种可能性:

  1. The idiomatic C++ way would be to use std::string , and the task becomes trivial: 惯用的C ++方式是使用std::string ,任务变得微不足道:

     class Student{ public: Student(int studentID, const std::string& studentName) : id(studentID), name(studentName) {} protected: std::string name; int id; }; 

    Then, when needed, you can get the underlying raw char array from name by calling the c_str member function: 然后,在需要时,可以通过调用c_str成员函数从name获取基础的原始char数组:

     const char* CStringName = name.c_str(); 
  2. If you want to use a char array instead, things get more complicated. 如果要改用char数组,事情会变得更加复杂。 You can first default-initialize the array and fill it in the constructor body with strcpy : 您可以首先默认初始化数组,并使用strcpy将其填充到构造函数主体中:

     class Student{ public: Student(int studentID, const char* studentName) : id(studentID) { assert(strlen(studentName) < 40); // make sure the given string fits in the array strcpy(name, studentName); } protected: char name[40]; int id; }; 

    Note that the argument char* studentName is identical to char studentName[40] , since you can't pass arrays as parameters by value, which is why the compiler just treats it as a char* pointing to the first char in the array. 注意参数char* studentName是相同的char studentName[40]因为你不能按值传递数组作为参数,这就是为什么编译器只是将其视为一个char*指向第一char在数组中。

You cannot implicitly copy arrays, they just do not have this feature. 您不能隐式复制数组,它们只是没有此功能。 Here is what you can do instead: 您可以改用以下方法:

The best option you have is safe the name in a std::string instead of an char[] . 最好的选择是使用std::string代替char[]来保证名称安全。 This would work just as your example, but could handle names of arbitrary length. 这将像您的示例一样工作,但是可以处理任意长度的名称。

Another alternative would be std::array<char, 40> . 另一种选择是std::array<char, 40> This is nearly the same as the char[] you use right now, but has the advantage of being copyable. 这几乎与您现在使用的char[]相同,但是具有可复制的优点。 It also would work with the code you showed. 它也可以与您显示的代码一起使用。 Unlike the string option, this would be trivially copyable, ie you can for example send and receive this as binary data. string选项不同,它可以轻松复制,例如,您可以将其作为二进制数据发送和接收。

If you really want or need to use the char[] , you need to copy over the string "by hand": 如果您确实需要或需要使用char[] ,则需要“手工”复制字符串:

Student::Student(int studentID ,char studentName[40]) : id(studentID){
    std::strcpy(name, studentName);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在初始化列表中初始化普通数组成员变量? - How can I initialize normal array member variable in initializer list? 如何使用 initializer_list 初始化成员数组? - How do I initialize a member array with an initializer_list? 如何在成员初始值设定项列表之外初始化类成员 - How to initialize class member outside the member initializer list 是否可以在 C++ 中的成员初始值设定项列表中初始化数组? - Is it possible to initialize an array in a member initializer list in C++? 使用初始化列表初始化成员数组的正确方法是什么? - What's the correct way to initialize a member array with an initializer list? 如何使用初始化列表初始化数组元素? - How to initialize array elements by using initializer list? 如何在C ++类的初始化列表中初始化member-struct? - How to initialize member-struct in initializer list of C++ class? 在类成员初始化器列表中初始化向量的元组 - Initialize a tuple of vectors in a class member initializer list 在类的成员初始化器列表中初始化std :: tuple - Initialize std::tuple in member initializer list of a class 如何在 C++ 初始值设定项列表中将常量引用成员初始化为另一个成员(标准向量) - How to initialize a const reference member to another member (std vector) in C++ initializer list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM