here is my problem.
I have a class that regularly modifies a char*.
There is another class, that needs to be able to read this value. So I want to pass the char* to the constructor of this second class, so that it can check the value, when required.
Let me give an example of the implementation that I have for another parameter, it is of a type boolean:
In ClassA:
bool f_valid = false; // global
m_eventCatcher.addProxy(porting::shared_ptr<CallbackProxy>(new handleCall(&f_valid)));
In ClassB:
struct handleCall
{
bool* m_dataValid;
handleCall(bool* result)
{
// saving the pointer to the boolean that I want to change
m_dataValid = result;
}
method()
{
if (smth)
{
(*m_dataValid) = false;
}
}
};
So far so good - this seems to work. Both classes can change and access this boolean.
Now I need to do the same thing with a char* (I cannot use string, so I guess this is a best way to store a short text, like a url address?).
So here is what I wrote:
ClassA:
const char* f_url = "blah blah"; // global
m_eventCatcher.addProxy(porting::shared_ptr<CallbackProxy>(new handleCall2(&f_url)));
ClassC:
struct handleCall2
{
char ** m_url;
handleCall2(char** url)
{
// saving the pointer to the char*
m_url= url;
std::cout << (*m_url) << std::endl; // prints out url fine
}
method()
{
std::cout << (*m_url) << std::endl; // by this time the value has been changed by ClassA, and I print out some rubbish - symbols, squares, etc.
}
};
I guess the problem is because the string has changed, its' address has changed too? I am getting really confused - can someone tell me what is going on, and what should I do in this situation?
UPDATE:
Looks like the problem is in HOW I modify the char*:
f_url = "new text"; // works fine
f_url = fileUrl.c_str(); // doesn't work! I get rubbish in the value when I try to access it from ClassB
strcpy(m_url, fileUrl.c_str()); // I also removed const from the variable and tried this - got a crash "access violation using location" :(
Is there any other way of writing the value of a string into a char *?