Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

NEWBIE: CListBox.GetItemDataPtr 'type cast' convertion error, pls help 1

Status
Not open for further replies.

Ominous

Programmer
Dec 3, 2000
1
CA
I created my own class to handle basic asset information. It is a bunch of strings. I first attach the data to a selection:
AnAsset temp;
temp.updateinfo(m_filename, m_desc, m_netver, m_localver, m_lastper, m_history);
AnAsset *ptemp = &temp;
m_list.SetItemDataPtr(m_list.GetCurSel(), ptemp);


That werks fine, but now I need to get the info back. It says I can't convert from 'void *' to 'class AnAsset' and I can't fingure out what I need to do. I tried type-casting it but that says I don't have proper constructor... Can't figure out what I am doin' wrong. Thx fer yer help...
Ominous
 
Ominous,

You need to understand 'scope' better. You can't use the address of a local variable the way your code does. Once that scope has exited the address is no longer valid. You need to do something like this:

AnAsset* pTemp = new AnAsset();
pTemp->updateinfo(....);
m_list.SetItemDataPtr(m_list.GetCurSel(), pTemp);

// to obtain the pointer
AnAsset* pTemp = (AnAsset*)m_list.GetItemDataPtr(...);

// or even better use the C++ runtime type cast operators
AnAsset* pTemp = reinterpret_cast< AnAsset* >(m_list.GetItemDataPtr(...));

Then of course you will need to clean up after the memory allocations.

delete pTemp;

Good luck
-pete


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top