a difficult question about the syntax of C++ pointers
Let me describe the problem. I have a piece of code that requires me to alter the address of one of 2 pointers that both point to the same class type. I have to select at runtime which pointer it is that will be manipulated depending on an input bool variable to the function. So i create a pointer to pointer variable in the function and then just point it to the appropriate pointer like so.
Object** pList;
if( bStatObj )
pList = &m_pStatObjList;
else
pList = &m_pDynObjList;
Using this i can manipulate what either of the other 2 pointers point to like this:
*pList = pObject;
What i would like to know is if it is possible to manipulate the contents of pObject using pList??? I tried the following and the compiler returns an error saying 'the left side of m_fMember requires class/union/struct.'
*pList->m_fMember = 2.0;
Thanks in advance for your time and effort ;p
Quote:Original post by bobbinus
*pList->m_fMember = 2.0;
You've almost got it. -> has a higher precedence than *. Add some brackets and it'll work:
(*pList)->m_fMember = 2.0;
Thanks a lot for those responses ;p
Just tried what u suggested and it works fine. Also reminded me that I knew that once and totally forgot about it!
BTW the second syntax Anonymous suggested returned the same error.
Just tried what u suggested and it works fine. Also reminded me that I knew that once and totally forgot about it!
BTW the second syntax Anonymous suggested returned the same error.
I believe you have to do this if you want to use the reference, as the second method Anonymous was trying to do.
*(*pList).m_fMember
(if not that, then "(*(*pList)).m_fMember")
David
*(*pList).m_fMember
(if not that, then "(*(*pList)).m_fMember")
David
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement