In a function parameter, a char[] (with or without a number specified) is just syntax sugar for a char* pointer. And you can't initialize a char[] array with a char* pointer, hence the compiler error.
For what you are attempting, you must copy the memory that the char* is pointing at, eg:
class C
{
char field[255];
public:
C(char field[255]) // aka C(char *field)
{
memcpy(this->field, field, 255);
// or: std::copy(field, field+255, this->field);
// or: std::copy_n(field, 255, this->field);
}
};
Alternatively, if you want to ensure the caller can only pass in a char[255], pass it by reference instead of by pointer:
class C
{
char field[255];
public:
C(const char (&field)[255])
{
memcpy(this->field, field, 255);
// or: std::copy(field, field+255, this->field);
// or: std::copy_n(field, 255, this->field);
}
};
That being said, you should consider using std::array instead, then you can use operator= assignment like you want:
#include <array>
using Char255Array = std::array<char, 255>;
class C
{
Char255Array field;
public:
C(const Char255Array &field)
{
this->field = field;
}
};
Or better, use the constructor's member initialization list:
#include <array>
using Char255Array = std::array<char, 255>;
class C
{
Char255Array field;
public:
C(const Char255Array &field)
: field(field)
{
}
};
std::stringinstead ofchararrays. Arrays can overflow. Thestd::stringcan be passed by reference.