Monday, 26 August 2013

Changing contents of a std::string with a function

Changing contents of a std::string with a function

I feel like this answer will be quick and simple, but I cannot seem to
figure it out right now.
#include <string>
#include <iostream>
using namespace std;
void change_thing (string x) {
x="not thing";
}
int main() {
string maybe_thing;
maybe_thing="thing";
change_thing(maybe_thing);
cout << maybe_thing << endl;
return 0;
}
I want maybe_thing to be "not thing" when it prints. I've tried a bunch of
different pointer strategies, but nothing seems to work (which I could
easily just be doing wrong; my knowledge of pointers is incomplete anyway
because I'm new to c++).
Thanks in advance!

No comments:

Post a Comment