M
Markus Dehmann
I guess this is a kind of newbie question (since most pointer questions are
newbie questions).
In the program below, modify(string* s) is supposed to change the content
that s points to. But the main() function shows that nothing is changed at
all!
What am I doing wrong?
#include <iostream>
#include <vector>
using namespace std;
class Modifier{
private:
vector<string> newStrings;
public:
void modify(string* s){
string newString = "test"; // toy example,
// could be *s transformed into upper case etc.
newStrings.push_back(newString);
s = &(newStrings[newStrings.size() - 1]); // point to new string
}
};
int main(){
Modifier m;
string s = "Hello World";
m.modify(&s);
cout << s << endl; // should be "test", but is still "Hello World"
return(EXIT_SUCCESS);
}
newbie questions).
In the program below, modify(string* s) is supposed to change the content
that s points to. But the main() function shows that nothing is changed at
all!
What am I doing wrong?
#include <iostream>
#include <vector>
using namespace std;
class Modifier{
private:
vector<string> newStrings;
public:
void modify(string* s){
string newString = "test"; // toy example,
// could be *s transformed into upper case etc.
newStrings.push_back(newString);
s = &(newStrings[newStrings.size() - 1]); // point to new string
}
};
int main(){
Modifier m;
string s = "Hello World";
m.modify(&s);
cout << s << endl; // should be "test", but is still "Hello World"
return(EXIT_SUCCESS);
}