all the parameter is passed by value,look at the program below
Code:
#include <iostream>
#include <string>
using namespace std;
void create(string* x)
{
string* _x = x;
cout << "adr x before: " << x << endl; // 0xbfb4c2fc
_x = new string("aaa");
cout << "adr x after: " << x << endl; // 0x804a008
}
int main()
{
string s;
cout << "adr s bevore: " << &s << endl; // 0xbfb4c2fc
create(&s);
cout << "adr s after: " << &s << endl; // 0xbfb4c2fc
return 0;
}
this program is easy to understand, when the value of x assign to _x, x has no relation with the function create.
your program is similar to it,and the compiler help you to do string* _x = x and _x = new string("aaa")
remember, all the paramter is passed by value
now , let's see another program
Code:
#include <iostream>
#include <string>
using namespace std;
void create(string** x)
{
cout << "adr x before: " << x << endl; // 0xbfb4c2fc
*x = new string("aaa");
cout << "adr x after: " << x << endl; // 0x804a008
}
int main()
{
string* s = NULL;
cout << "adr s bevore: " << &s << endl; // 0xbfb4c2fc
create(&s);
cout << "adr s after: " << &s << endl; // 0xbfb4c2fc
return 0;
}
this program work perfectly, why?let's modify the function as the compiler always help us
void create(string** x)
{
string** _x = x;
*_x = new string("aaa");
}
_x and x are point to the same area of the memory, so *_x is *x