Should I use static_cast or reinterpret_cast when casting a void* to whatever
Both static_cast and reinterpret_cast seem to work fine for casting void* to another pointer type. Is there a good reason to favor one over the other? ...
Both static_cast and reinterpret_cast seem to work fine for casting void* to another pointer type. Is there a good reason to favor one over the other? ...
Can anyone tell me what this cast has for effect (besides setting happyNumber to 1337), if any at all, and if it has no other effect, how come I can write code like this??? Is this a compiler bug, or some "hidden away feature" of C++? int happyNumber = static_cast<int>(123.456, TRUE, "WTF" , false , "IS" , NULL , "GOING" , 0xff , "ON???...
What is the equivalent of a static_cast with boost::shared_ptr? In other words, how do I have to rewrite the following Base* b = new Base(); Derived* d = static_cast<Derived*>(b); when using shared_ptr? boost::shared_ptr<Base> b(new Base()); boost::shared_ptr<Derived> d = ??? ...
Hi, What does the following syntax mean? typedef void* hMyClass; //typedef as a handle or reference hMyClass f = &something; const MyClass& foo = static_cast<MyClass&>(*f); foo.bar(); ...
What is implicit_cast? when should I prefer implicit_cast rather than static_cast? ...
How can I convert the following struct to unsigned char*? typedef struct { unsigned char uc1; unsigned char uc2; unsigned char uc3; unsigned char uc5; unsigned char uc6; } uchar_t; uchar_t *uc_ptr = new uchar; unsigned char * uc_ptr2 = static_cast<unsigned char*>(*uc_ptr); // invalid static cast at the previous line...
If I have a derived class with multiple bases, each this pointer for each base will be different from that of the derived object's this pointer, except for one. Given two types in an inheritance hierarchy, I'd like to detect at compile time whether they share the same this pointer. Something like this should work, but doesn't: BOOST_STA...
I have a class hierarchy where I know that a given class (B) will always be derived into a second one (D). In B's constructor, is it safe to statically cast the this pointer into a D* if I'm sure that nobody will ever try to use it before the entire construction is finished? In my case, I want to pass a reference to the object to yet ano...
I saw one book on C++ mentioning that navigating inheritance hierarchies using static cast is more efficient than using dynamic cast. Example: #include <iostream> #include <typeinfo> using namespace std; class Shape { public: virtual ~Shape() {}; }; class Circle : public Shape {}; class Square : public Shape {}; class Other {}; int ...
Is there any reason to prefer static_cast<> over C style casting? Are they equivalent? Is their any sort of speed difference? ...
When I try to use a static_cast to cast a double* to an int*, I get the following error: invalid static_cast from type ‘double*’ to type ‘int*’ Here is the code: #include <iostream> int main() { double* p = new double(2); int* r; r=static_cast<int*>(p); std::cout << *r << std::endl; } I understand ...
I have an array of custom class Student objects. CourseStudent and ResearchStudent both inherit from Student, and all the instances of Student are one or the other of these. I have a function to go through the array, determine the subtype of each Student, then call subtype-specific member functions on them. The problem is, because thes...
Hi, I was answering a question a few minutes ago and it raised to me another one: In one of my projects, I do some network message parsing. The messages are in the form of: [1 byte message type][2 bytes payload length][x bytes payload] The format and content of the payload are determined by the message type. I have a class hierarchy...
I am trying to understand this example code regarding Browser Helper Objects. Inside, the author implements a single class which exposes multiple interfaces (IObjectWithSite, IDispatch). His QueryInterface function performs the following: if(riid == IID_IUnknown) *ppv = static_cast<BHO*>(this); else if(riid == IID_IObjectWithSite) *pp...
So if your converting from Void* to Type* or from Type* to Void* should you use: void func(void *p) { Params *params = static_cast<Params*>(p); } or void func(void *p) { Params *params = reinterpret_cast<Params*>(p); } To me static_cast seems the more correct but I've seen both used for the same purpose. Also, does the dir...
I need to write code for a callback function (it will be called from within ATL, but that's not really important): HRESULT callback( void* myObjectVoid ) { if( myObjectVoid == 0 ) { return E_POINTER; } CMyClass* myObject = static_cast<CMyClass*>( myObjectVoid ); return myObject->CallMethod(); } here the void* is...
Possible Duplicate: Regular cast vs. static_cast vs. dynamic_cast what is difference between static and dynamic cast in c++? ...
Edit - Put the question into context a bit more. Given: struct Base { ... }; struct Derived : public Base { ... }; class Alice { Alice(Base *const _a); ... }; class Bob : public Alice { Bob(Derived *const _a); ... }; When I try to implement Bob::Bob(Derived *const _d) : Alice(static_cast<Base*const>(_d)) { } ...