Passing pointers to methods - all of it this time!

E

Equus

Hi - sorry! My previous message got screwed up, evidently :-$

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::myMethod(param a, toBeUpdated* b)
{

if(a == myDataMember) {
b = &someData;
return true;
}
else return false;
}


I call it via a pointer to an object:

myClass* s;
toBeUpdated* updated = 0;

if( s->myDataMember(a,updated) ) {
cout << updated->something << endl; // SEGFAULTS!
}
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.
Am I missing a trick, or is this a bug in gcc?

Thanks,

Chris
 
J

Johannes Bauer

Equus said:
if(a == myDataMember) {
b = &someData;

Use're using call-by-value here! What you mean is

*b = &someData
cout << updated->something << endl; // SEGFAULTS!

Otherwise "updated" will stay NULL - and segfault.

Greetings,
Johannes
 
V

Victor Bazarov

Equus said:
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::myMethod(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::myMethod' 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::myMethod(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::myMethod(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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top