Equus wrote:
Hi - sorry! My previous message got screwed up, evidently :-$
Aha...
I have a weird case of a pointer, passed as an argument to a method,
not being assigned to the value it should.
My method is like this:
bool myClass::myMeth od(param a, toBeUpdated* b)
{
if(a == myDataMember) {
b = &someData;
Here you update the local object, the argument. Right after the
function returns 'b' is destroyed, any changes to 'b' are lost.
return true;
}
else return false;
}
I call it via a pointer to an object:
myClass* s;
toBeUpdated* updated = 0;
You probably initialise 's' to something, don't you...
>
if( s->myDataMember(a ,updated) ) {
You mean
if (s->myMethod(a, updated) ) {
cout << updated->something << endl; // SEGFAULTS!
'updated' is still null here.
}
else {
cout << "Did not return anything in 'updated'" << endl;
}
Now, the issue is that for some reason, when the method returns true,
the value assigned to b in the method does not make it out - in other
words, the value of 'updated' is not changed by calling the method.
Correct.
Am I missing a trick, or is this a bug in gcc?
There is no trick, there is no bug. You assign a new value to the
variable that exists only in 'myClass::myMet hod' function (the local
variable initialised from the argument).
If you want to change something from the outside, you need to either
pass it by reference or pass a pointer to _it_ and dereference when
assigning. So, you can do
bool myClass::myMeth od(param a, toBeUpdated* & b)
// note the & here ^^^
{
if(a == myDataMember) {
b = &someData;
return true;
}
else return false;
}
...
if (s->myMethod(a, updated) ) {
or
bool myClass::myMeth od(param a, toBeUpdated* * b)
// note the * here ^^^
{
if(a == myDataMember) {
*b = &someData;
// ^^^ Note the * here as well
return true;
}
else return false;
}
...
if (s->myMethod(a, &updated) ) {
// Note the & here ^^^
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask