c++ - Structure tag and name, why does a local variable declared as name compile? -
in code saw there structure defined this:
typedef struct tagmystruct { int numberone; int numbertwo; } mystruct; the way understand this, tagmystruct new data type , mystruct variable created right there.
at place, used this:
mystruct *pstruct = new mystruct; and compiled fine visual studio 2010. how valid c++? thought mystruct variable , not type?
no. tagmystruct name of struct. in c, unlike c++, must explicitly use struct keyword every time use struct type. example
tagmystruct x; //error struct tagmystruct x; //ok to avoid writing struct time, struct tagmystruct typedef'd mystruct. can write
mystruct x; //ok, same struct tagmystruct x; what thought (a variable definition) without typedef keyword, this
struct tagmystruct { int numberone; int numbertwo; } mystruct; btw
mystruct pstruct = new mystruct; //error cannot convert mystruct* mystruct is not valid c or c++ anyway. maybe mean
mystruct* pstruct = new mystruct; //valid c++ (invalid c - use malloc instead of new in c) hth
Comments
Post a Comment