2 quick questions about passing by const pointer/const reference?

Const LPCREATESTRUCT expands to CREATESTRUCT* const i.e. A constant pointer to non-const structure, while const CREATESTRUCT is a non-constant pointer to constant structure, so the compiler is right.

Const LPCREATESTRUCT expands to CREATESTRUCT* const, i.e. A constant pointer to non-const structure, while const CREATESTRUCT* is a non-constant pointer to constant structure, so the compiler is right.

It shouldn't, any standard-compliant compiler makes that an error because ms points to a const my_structure. Edit: With your updated case, its a problem of how typedefs work: typedef T* TPtr; void f(const TPtr); void g(T* const); void h(const T*); Both f() and g() are equivalent and take a const pointer to a T, while h() a pointer to a const T.

Yea, I tested myself. The code I posted actually didn't compile so I edited with my own code – Marlon Feb 18 '10 at 3:29.

Assuming you have some control over the signature of the OnCreate method, I would suggest writing it as follows void OnCreate(HWND hWnd, CREATESTRUCT& createStruct) { createStruct. X = 2; // this compiles } or, if you don't want to allow modification, then void OnCreate(HWND hWnd, const CREATESTRUCT& createStruct) { createStruct. X = 2; // this does not compile } In addition to avoiding any confusion about exactly what is const, by using C++ references you've also said in code that the argument "can't" be NULL.(Yes, somebody could write OnCreate(hWnd, *(static_cast(NULL))) to create a null reference).

Related Questions