If you need to use a text field in a class in private, and write a public method to get the value of this field, how best to do it? Now I try to use char* pole or char pole[10]; for storage char pole[10]; If I understand correctly, it is impossible to return the char array, and then how to be?

 #include <stdio.h> class MyClass { public: int getPole1() { return pole1; } void setPole1(int n) { pole1 = n; } //здесь должны быть аналогичные методы для поля pole2 private: int pole1; char pole2[10]; }; int main() { MyClass my; //здесь я должен установить значение pole2 и затем его получить return 0; } 
  • Please tag correctly. First of all, you don’t have Sy here. Secondly, it is not necessary to put labels on the principle "all that is in the code." - Pavel Mayorov
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky
  • There are no properties or methods in c++ . There are member data and member functions. - αλεχολυτ
  • @PavelMayorov explain, please, why c ++, not c, if I don’t use c ++ libraries, but only stdio? I actually have a clean C and need to use, not with ++. - miketyson
  • On "pure C" there are no classes and methods. - Pavel Mayorov

3 answers 3

If you have a training task, then you must use the type const char * (aka char const * ) to pass the string. A character array can be implicitly cast to this data type.

But it’s impossible to store a string in a class in the form of a pointer, because the lifetime of a class instance and the lifetime of a string may not be the same. Therefore, it is necessary to store an array of characters in the class, and when writing the value of the property, you must copy the string from the parameter to the array either in a loop or with the help of strncpy .

If you have a real task, then connect <string> and just use std::string .

  • You can store a pointer, you just need to manage it correctly. Implement the appropriate kit (rule of three). - αλεχολυτ

If char* critical one, not string , then I would do the class like this:

 class MyClass { public: .... const char * getPole2() const { return pole2; } void setPole2(const char * p) { delete[] pole2; pole2 = (p) ? new char[strlen(p)+1] : nullptr; if (pole2) strcpy(pole2,p); } ~MyClass() { delete[] pole2; } private: int pole1; char * pole2 = 0; }; 

But using the string member is simpler.

    All attempts to implement reading and setting the string data type as a member of the class will ultimately lead to the creation of its own analogue std::string . Therefore, it is not necessary to reinvent the wheel and step on a rake, but it is worth using the standard, proven solution.

    At the same time, nesting in MyClass also a string functional leads to a superclass that will serve both string handling, and working with integer numbers, and maybe more with something in the future. This will complicate the further use and debugging of this class. Therefore, all work with strings (even if you want to do it yourself without using std::string ) must be placed in a separate class.

    If the mentioned member pole2 should always be known at the time of compiling the size (for example, as you have 10 characters in the example), then it is more logical to use the class std::array instead of std::string . Getters and setters in this case will be implemented elementary, as well as using std::string , then the overhead will be less.