   [Solved] Is it legal to cast "this" pointer?
  I'm getting an error on the following line of code:iTypeManager* pointer() {return static_cast<iTypeManager*>(this);}Is this not a legal cast, and if not is there an easy fix?(I can do without it, but it does make it all a bit neater)For reference, here is the class declaration:#include "iTypeManager.hpp"using namespace std;class dMessage:public iTypeManager{public:    string getID(){return "message";}    iTypeManager* pointer() {return static_cast<iTypeManager*>(this);}};[Edited by - methinks on June 19, 2006 6:50:18 PM]
  In that case, because dMessage is derived from iTypeManager, you should just be able to "return this" without a cast at all.Your code, while not needed, seems like valid syntax to me though.. what error do you get?   ;  If the whole point of this 'pointer' function is to get a pointer to the base class, then shouldn't it simply be a method of the base class?Or are you overriding a base class implementation to change the functionality?   ;  Did you try reinterpret_cast?   ;Quote:Original post by RoboguyDid you try reinterpret_cast?I hope not - reinterpret_cast is not a good idea to use on class pointers. If the base class has any virtual functions (which it is very likely to do) then a reinterpret_cast can lead to attempts to read the wrong v-table for certain cases, such as multiple inheritance. The result will, in the best case, segfault the program and cause it to crash; in the worst case, undefined chaos will occur, and there's a good chance of data getting clobbered.The correct cast to use to move between levels of an inheritance hierarchy is dynamic_cast - and be sure to check for a null-pointer return in case of failure (or catch the std::bad_cast exception if working with references).As for the original post: the pointer() function is a waste; you can get the pointer using the & (address-of) operator. So instead of:iTypeManager* p = object.pointer();You write this:iTypeManager* p = &object;As a bonus, you do not need to cast here if you are moving up the inheritance hierarchy - e.g. if object is of type dMessage, the language will automatically downcast from dMessage* to iTypeManager* safely.   ;  Got it fixed, I was casting it backwards (See what happens when you code too late at night?)Just to clarify: the reason that I needed to cast was that the base class was just for storage (Storing an array of pointers to base), the inherited classes have additional functionality that needs to be accessable.Thanks for all the hints and thoughts.   ;Quote:Original post by ApochPiQI hope not - reinterpret_cast is not a good idea to use on class pointers. If the base class has any virtual functions (which it is very likely to do) then a reinterpret_cast can lead to attempts to read the wrong v-table for certain cases, such as multiple inheritance. The result will, in the best case, segfault the program and cause it to crash; in the worst case, undefined chaos will occur, and there's a good chance of data getting clobbered.The correct cast to use to move between levels of an inheritance hierarchy is dynamic_cast - and be sure to check for a null-pointer return in case of failure (or catch the std::bad_cast exception if working with references).Could you or anyone provide some documentation on this, because I would like to know more about it. Is this undefined behavior compiler dependant?   ;Quote:Original post by owlCould you or anyone provide some documentation on this, because I would like to know more about it. Is this undefined behavior compiler dependant?That's the definition of "undefined" in this context.  There is no standard behavior, and yet it has to do something.  That something then depends on the implementation (compiler).;  The C++ standard makes very few guarantees about reinterpret_cast.  With the exception of certain casts from X * to Y to X * which are guaranteed to not to change the pointer value (with certain constraints on Y) and most casts of function pointers and pointers to member functions (where calling via the cast pointer results in undefined behaviour) the behaviour of reinterpret_cast is entirely implementation-defined.Note that there is a difference between undefined behaviour and implementation-defined behaviour, loosely that in the former case the implementor is allowed to not know the resulting behaviour whereas in the latter case the actual behaviour must be known and should be documented by the implementor.Σnigma   ;  What does reinterpret_cast actually do? (I mean internally)And what about casting from void? Does it suffer the same problem with class objects?   