Const input parameters and typedefs


 
In good API design, one specifies unchangeable data that’s passed by pointer as const:
 
   void foo( const Stuff* pConstantStuff );
 
The compiler will guarantee that the "Stuff" pointed to by pConstantStuff is not modified by foo(). This is goodness. Microsoft APIs have a habit of using typedefs for structures, ala:
 
   void foo( const STUFF* pConstantStuff );
 
In fact, some people even use typedefs for pointers to structures:
 
   typedef Stuff* PSTUFF;
 
And they write the API like this:
 
   void foo( const PSTUFF pConstantStuff ); // Evil; this does not do what you expect!
 
That last version is not code you ever want to write! What it seems to promise is not what it actually promises. The compiler interprets it as:
 
   void foo( Stuff* const pConstantStuff ); // weird but technically correct
 
This version guarantees that the pointer itself cannot be changed by the function. However, the contents of pConstantStuff are no longer constant. The code can change the contents and the compiler will happily oblige! There are two solutions. Solution one is to create a const version of the typedef and use it where appropriate. Here’s how winnt.h does it for strings:
 
   typedef char *LPSTR;
   typedef const char* LPCSTR;
 
The preferable solution is to avoid typedefs for pointer types and use const directly whenever you need it. Use const whenever you possibly can. In return, you get readable code that clearly expresses the intent — and the compiler properly enforces the intent.
 
Advertisements
This entry was posted in C++. Bookmark the permalink.

One Response to Const input parameters and typedefs

  1. Unknown says:

    You should know that const is a postfix modifier. So, the right way is to define a pointer to const stuff is to write like thisvoid foo( Stuff const* pConstantStuff );When you whitevoid foo( const PSTUFF pConstantStuff );or (that is better)void foo( PSTUFF const pConstantStuff );it is equivalent tovoid foo( Stuff * const pConstantStuff );And you should understand that function takes const pointer.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s