c++ - Casting from unknown pointer to class; how to check validity? -


i using lparam (essentially long, unless we're on 64-bit system in case long long) member of lvitem (called lparam) store pointer object coincides entry in listview.

when want edit item, want cast lparam myclass*, works fine long lparam correctly set equal myclass* in first place, i'd kind of checking make sure is, in fact, myclass number pointing to.

currently have this:

lvitem lv; // lv filled in lvm_getitem classptr = static_cast<myclass*>((void*)lv.lparam); if ( !classptr )     return false; 

now, i'm not clear: static_cast return null if argument not valid pointer? assume not, since that's dynamic_cast for, i'm not entirely certain. and, if correct in assumption, there way check classptr valid before attempt access members , cause crash...

you can't check @ in pure standard c++. can probabilistic check using windows api. so, in practice can guarantee it, making code correct.

if check still not know the correct myclass instance.

so, way make code guaranteed correct. that's easier when limit access things. c++ has many features limit access, e.g. const; use them.

cheers & hth.,


Comments

Popular posts from this blog

c++ - Is it possible to compile a VST on linux? -

java - Output of Eclipse is rubbish -

jquery - Confused with JSON data and normal data in Django ajax request -