Why is a member variable not passed to the modified function?

I'm still learning C ++, trying to figure out how this works, and came across something that puzzles me.

I have a class that just contains list<string>and has several member functions

class String_list {
      public:
            String_list(istream&);
            //other functions
            list<string> listing;
};

and I wrote my constructor to use a non-member function

String_list::String_list(istream& in) { get_strings(in, listing); }

with

istream& get_strings(istream& in, list<string> lstring)
{
  if(in) {
       lstring.clear();

       string word;

       while (in >> word) 
               lstring.push_back(word);

       in.clear();
       }
  return in;
 }

The problem is that the function get_stringseems to work, it does not change the element variable listingthat I passed to it.

, get_strings - (istream& in), lstring listing, , - , , , -, , .

, , .size().

+5
3

- . , - .

get_strings, , :

istream& get_strings(istream& in, list<string>& lstring)
{
   // ...
}

, ( - ). get_strings , , . :

list<string> get_strings(istream& in)
{
   list<string> r;
   // ...
   return r;
}

:

StringList::StringList(istream& in) : listing(get_strings(in))
{
   // ...
}
+9

lstring :

istream& get_strings(istream& in, list<string>& lstring)

, : lstring, . , .

+10

++ , , . , :

istream& get_strings(istream& in, list<string> lstring) 

lstring , , , . , , .

If you want to change the transferred object, you must pass it by the link that you can reach using &:

istream& get_strings(istream& in, list<string>& lstring) 
+4
source

All Articles