Using a string reference

I have a class like this. I want to use a link for a string, but it does not work. How can I use string &

#include <string> using namespace std; class T { public: T(string& s); private: string s; }; T::T(string& s) { this->s = s; } int main(void) { T t("Test Object"); return 0; } 

Error: 'T :: T (std :: string &)': cannot convert parameter 1 from 'const char [12]' to 'std :: string &'

+4
source share
5 answers

Use const :

 class T { public: T(const string& ss); private: string s; }; T::T(const string& ss) : s(ss) { } 

"Test Object" will be constructed as a const string before moving on to the T constructor, so the constructor must accept a const string .

+5
source

You do not pass in std :: string. As said, it cannot convert from const char array to string-ref. Modify the constructor to execute const-ref and it will work. This is because the temporary string must be created from char -array, and you can only make const references in the temporary order to stop you from getting confused (if you change the temporary, the changes will simply be discarded, so the language stops you from doing this).

+2
source

In C ++ 98/03 , if you have a class that is not cheap to copy (for example, int or double cheap to copy, a std::string isn 't since its copy may include allocating new heap memory, copying characters from the source to the destination, etc.), then the rule must follow the const link const std::string& :

 class T { public: T(const string& s); // <--- const string& private: string m_s; }; 

And then in the constructor do:

 T::T(const string& s) : m_s(s) {} 

However, in C ++ 11 , where movement semantics are available, the new rule looks like this: if you need a copy (and the object is cheap to move, as it usually should be), go by value and go from value:

 T::T(string s) // pass by value : m_s( std::move(s) ) // and move from the value {} 

(It would be optimal to offer a couple of overloads, passing by const & and passing by value, but probably this is not necessary in all applications, but only when you need to compress performance.)

Please note that when you do not need a copy and just need to observe this parameter, the usual C ++ 98/03 pass const & code remains valid.

+1
source

Links must be initialized using the constructor initialization list. Change your constructor to:

 T::T(string& s) : s(s) { } 

Additionally, specify your s member as std::string& s to be able to reference.

Perhaps change the name s to avoid ambiguity.

See this entry in SO.

0
source

The constructor being defined takes std::string by reference:

 T::T(std::string& s) { this->s = s; } 

Thus, the simplest task would be to create a std::string object that will be passed to this constructor:

 std::string s("Test Object"); T t(s); 

But since your constructor does not change std::string , you pass it (it is just used to set the value of the data element T ), you must pass the link const : T::T(const string& s) . In addition, instead of allowing the creation of the data element s and assigning it another row, it would be better if you create this element directly in the initialization list:

 T::T(const std::string& str) : s(str) { } 
0
source

All Articles