[英]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 ++中分配数组,因此您基本上有两种可能性:
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();
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.