Sign in to follow this  

a difficult question about the syntax of C++ pointers

This topic is 4662 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
try (*pList)->m_fMember
or **pList.m_fMember

Share this post


Link to post
Share on other sites
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;

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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

Share this post


Link to post
Share on other sites

This topic is 4662 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this