What is const and why is it needed here? Why is no string passed to a function without const?

class Object { private: string stroka; public: Object::Object(string &n) { Object::stroka = n;} } void main() { Object FAI("ss"); } 
  • class Object {private: string stroka; public: Object :: Object (string & n) {Object :: stroka = n;}} void main () {Object FAI ("ss"); } The compiler swears. - aleshka-batman
  • your code is strange ... ; not exactly missed? Why does Object:: write every time? - pavel

3 answers 3

A string ( std::string ) is passed to the function with and without const , but the C-string (just the text between the quotes, for example "Text" ) cannot be passed without const , because a temporary object std::string will be created This will be initialized with a C string. A constant link can and is attached to a temporary object, whereas a non-constant link cannot be attached to a temporary object.

I advise you to read about links and constants, this is the basis of C ++.

    Possible without const , depends on what the function should do.

    The const modifier means that the string that goes into the function will not be changed inside this function.

    If the function must modify the string (for example, this is a function to remove spaces), then const must not be set (otherwise it will not be possible to modify the string).

    If, on the contrary, the function should not modify the string (for example, this function returns the value by string as a key), then of course it will work without const - but it is better to put const . In this case, if the function code by mistake still tries to modify the string, it will not be compiled. It helps to catch possible mistakes.


    Thus, we can consider const kind of documentation: it indicates whether a parameter can be changed within a function.


    If you have a const string in hand, then you, of course, will not be able to pass this to a function that accepts just a string or there string& . The reason for this - const string - is a string that can not be changed, but the declaration of a function without const means that the function can change the string. To change it was impossible, the compiler will not allow you to compile it.

    • Why the compiler swears when I pass the string class Object {private: string stroka; public: Object :: Object (string & n) {Object :: stroka = n;}} void main () {Object FAI ("ss"); } - aleshka-batman
    • @ aleshka-batman: Well, show the code and give your exact error. (And read the response update.) - VladD

    ixSci Told you all right.

    About this:

    Why the compiler swears when I pass the string class Object {private: string stroka; public: Object :: Object (string & n) {Object :: stroka = n;}} void main () {Object FAI ("ss"); }

    You are trying to push a constant into a non-constant. And you probably see the error conversion when you pass const char * ("foo") to Object (string & n) const std :: string will be implicitly created.

    If you create an object like this

      string f("ss"); Object FAI(f); 

    That will give a ride. ps But it is better to pass a string by a constant link.