Convert DWORD_PTR back to my object
-
I have problem with Casting ... i've create class named Instrument { ... } and in Dialog based MFC application i have TreeView Control and in that at some point i am settin ItemData as "Instument" classs' Object treeCtrl->SetItemData(instObject)//Instrument* to DWORD_PTR casting is done here ;) now at some point i need this data(object) back so to take data Instrument *instObject = treeCtrl->GetItemData(hItem); // error: :^) Please help me to cast DWORD_PTR back to Instrument Type ...:(
-
I have problem with Casting ... i've create class named Instrument { ... } and in Dialog based MFC application i have TreeView Control and in that at some point i am settin ItemData as "Instument" classs' Object treeCtrl->SetItemData(instObject)//Instrument* to DWORD_PTR casting is done here ;) now at some point i need this data(object) back so to take data Instrument *instObject = treeCtrl->GetItemData(hItem); // error: :^) Please help me to cast DWORD_PTR back to Instrument Type ...:(
To reverse the effect of the first cast, cast the value back to the original type:
Instrument *instObject = (Instrument*) treeCtrl->GetItemData(hItem);
--Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ "That's what's great about doing user interface work. No matter what you do, people will say that what you did was idiotic." -- Raymond Chen
-
To reverse the effect of the first cast, cast the value back to the original type:
Instrument *instObject = (Instrument*) treeCtrl->GetItemData(hItem);
--Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ "That's what's great about doing user interface work. No matter what you do, people will say that what you did was idiotic." -- Raymond Chen
hey its not working ... otherwise i would have done that ...
-
hey its not working ... otherwise i would have done that ...
Then you've obviously done something else wrong, as Michael's suggestion is correct. How are you verifying that it does not work?
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
Then you've obviously done something else wrong, as Michael's suggestion is correct. How are you verifying that it does not work?
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
I'm guessing you are getting cannot convert DWORD_PTR to Instrument *. Oh and allways try and use eplicit casting i.e. static_cast rather than C-Style casts. Try this Instrument *instObject = static_cast((static_cast(treeCtrl->GetItemData(hItem)))); or if not Instrument *instObject = dynamic_cast((dynamic_cast(treeCtrl->GetItemData(hItem)))); Could you copy the exact compiler error into the thread.
-
Then you've obviously done something else wrong, as Michael's suggestion is correct. How are you verifying that it does not work?
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
oh sorry its working now ... it was my mistake ... Thanks a lot ... :-D
-
I'm guessing you are getting cannot convert DWORD_PTR to Instrument *. Oh and allways try and use eplicit casting i.e. static_cast rather than C-Style casts. Try this Instrument *instObject = static_cast((static_cast(treeCtrl->GetItemData(hItem)))); or if not Instrument *instObject = dynamic_cast((dynamic_cast(treeCtrl->GetItemData(hItem)))); Could you copy the exact compiler error into the thread.
reinterpret_cast
is the right one for this situation, since you're converting between a pointer and a non-pointer type.--Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ I work for Keyser Söze