Archived

This topic is now archived and is closed to further replies.

BlueChip

Simple pointers question..

Recommended Posts

Hi... I''ve a question: if I''ve these methods of a XXX class: void SetBoolLink (bool link) { m_bBoolLink = new bool; *m_bBoolLink = link;} and void SwitchBoolLink () { if (*m_bBoolLink==true) (*m_bBoolLink)=false; else (*m_bBoolLink)=true;} and I do this: bool aBoolVariable = false; xxxobject->SetBoolLink(&aBoolVariable); xxxobject->SwitchBoolLink() ; should aBoolVariable to be TRUE? In my opinion yes, but for my program no... after above code, *m_bBoolLink is TRUE but aBoolVariable is FALSE. Why? If *m_bBoolLink is a pointer to aBoolVariable, then *m_bBoolLink=true is as aBoolVariable=true... or no? thanks.... bye bye

Share this post


Link to post
Share on other sites
m_bBoolLink = new bool;

Creates a new boolean instance, which means the XXX classes pointer isnt referencing the boolean you passed as the parameter.

try this:

void SetBoolLink (bool *link) { m_bBoolLink = link;}

and you can shorten the second half to:

void SwitchBoolLink () { *m_bBoolLink = !(*m_bBoolLink ); }

[edited by - LowCalorieSoftDrink on May 8, 2003 9:30:50 AM]

Share this post


Link to post
Share on other sites