1

If I create my class like this:

class MyCLass {
private:
    char name[25]{};
public:
    MyClass();
    MyClass(char name[]);
};
MyClass::MyCLass() {
    stringCopy("", this->name);
}
MyCLass::MyClass(char name[]) {
    stringCopy(name, this->name);
}

where stringCopy is a function I made to copy two char arrays, when I create in the main function an object MyClass obj("John");, the name is assigned correctly but I get the following warning: ISO C++11 does not allow conversion from string literal to 'char *', so I asked myself: What is the correct and best way to initialize a char array as a class member?

First of all, I don't like the way I initialized char name[25]{}; but the only alternative would be to use dynamic memory. Is there a better alternative than the two?

Also, although the message I get after compiling the code is only a warning, I'd rather avoid such potential conflicts in my code. Is there another way to avoid this problem other than creating a char array with the name and then creating an object and passing this new variable as an argument?

EDIT: I missed that detail: I am required to use a char array. No strings are allowed.

Kotaka Danski
  • 451
  • 1
  • 4
  • 14
  • 2
    The constructor `MyCLass::MyClass(char name[])` needs to be `MyCLass::MyClass(const char name[])`. A string literal is a `const char[]`. – François Andrieux Mar 10 '21 at 18:51
  • 3
    In C++ for your own sanity use `std::string`. Using C strings is nothing but a mess, especially in situations like this where you have a length limit (`25`) that is not advertised anywhere, nor documented, nor easily discoverable. – tadman Mar 10 '21 at 18:52
  • If you must use character arrays, please review the `str*()` standard functions, such as `stdcpy` and `strcmp`. Otherwise use `std::string`. – Thomas Matthews Mar 10 '21 at 19:03
  • @tadman I totally agree with you but using cString is a requirement for this assignment. Also, creating my own stdcpy functions is encouraged. – Kotaka Danski Mar 11 '21 at 07:10
  • @FrançoisAndrieux That answered one of my questions. Thanks! – Kotaka Danski Mar 11 '21 at 10:02
  • These "C++" courses are causing lasting damage to the industry. Sad to see they keep coming. – tadman Mar 11 '21 at 21:00
  • @tadman I actually study at one of the best universities in my country. I am not a great fan of such torture but it isn't completely useless. – Kotaka Danski Mar 13 '21 at 10:01
  • Sadly the best universities are still teaching C++ material that was out of date in the early 2000s. The never have been known for being "current". – tadman Mar 14 '21 at 23:36

1 Answers1

0

Given the advices in the comments, you would probably go with something along:

class MyCLass {
private:
    std::string name;
public:
    MyClass(std::string & n = ""):name(std::move(n)){};
};

There is not point in using a StringCopy() function for this kind of copy, the functions in the standard libary do that well already ()

Heyji
  • 1,113
  • 8
  • 26