Tuesday, May 4, 2010

Are C++ Raw Pointers First Class Objects?

I had this doubt nagging me for a long time whether C++ raw pointers are first class objects or not. Wikipedia had a certain definition about it and C++ raw pointers seemed to satisfy all the requirements. PPD (our Head of Engineering) had taken a lecture on RAII (Resource Acquisition Is Initialization) principle some time back and in that context he had suggested that C++ raw pointers are not first class objects. I wasn't fully sure whether I understood this well or not. So I raised this question on Stack Overflow (http://stackoverflow.com/questions/2695169/are-raw-c-pointers-first-class-objects).

It turned out that different people had different opinions about it. Some even trashed the Wikipedia definition. Finally I got PPD also on this discussion to provide a clear perspective on this. His answer can be seen from the above post in detail.

In short both "Pointers are first class objects" and "Pointers are not first class objects" statements are true. It depends purely on the context in which we are discussing about them.
If we are using the pointers merely to point to some data (some object) then they are first class object. If we are using the pointers to hold the data (hence have an ownership semantics attached to them) then they are not first class objects.

For more in depth explanation, read on the stack overflow link above.

No comments: