inheritance - Unhandled exception - c++ program stops on cast -


i have several structs:

struct token {  //some content }  follows bunch of structs inherit token: struct : public token{  //stuff }  . . . struct z : public token{  //other stuff } 

i have vector std::vector filled subclasses through z , program crashes when try cast element in the vector subclass. i'm casting doing following:

a subclass = *((a * ) &vector[0]); 

what doing wrong?

you should use dynamic_cast when casting pointers 1 type in use case. 1 using c style cast , suggest go dynamic_cast. code should like:

if(dynamic_cast<a *>(vector[0]))     subclass = *(dynamic_cast<a *>(vector[0])); 

when dynamic_cast fails return null pointer , should take care of appropriately. refer dynamic_cast , static_cast in c++ more information. additionally when should static_cast, dynamic_cast, const_cast , reinterpret_cast used? understand lot more types of casts.


Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -