Skip to main content
GameDev.net gamedev.net
🔒 Locked

Simple pointers question..

Started by BlueChip May 8, 2003 at 8:23 AM 1 replies 250+ views
Original Post
BlueChip
BlueChip
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
LowCalorieSoftDrink
LowCalorieSoftDrink
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]
BlueChip
BlueChip
thank you to your help =)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.