I looked through some code and noticed that the convention was to turn pointer types like
SomeStruct*
into
typedef SomeStruct* pSomeStruct;
Is there any merit to this?
I looked through some code and noticed that the convention was to turn pointer types like
SomeStruct*
into
typedef SomeStruct* pSomeStruct;
Is there any merit to this?
Not in my experience. Hiding the '*
' makes the code hard to read.
This is a matter of style. You see this kind of code very frequently in the Windows header files. Though they tend to prefer the all upper case version instead of prefixing with a lower case p.
Personally I avoid this use of typedef. It's much clearer to have the user explicitly say they want a Foo* than PFoo. Typedef's are best suited these days for making STL readable :)
typedef stl::map<stl::wstring,CAdapt<CComPtr<IFoo>> NameToFooMap;
This can be appropriate when the pointer itself can be regarded as a "black box", that is, a piece of data whose internal representation should be irrelevant to the code.
Essentially, if your code will never dereference the pointer, and you just pass it around API functions (sometimes by reference), then not only does the typedef reduce the number of *
s in your code, but also suggests to the programmer that the pointer shouldn't really be meddled with.
This also makes it easier to change the API in the future if the need arises, for instance using an ID rather than a pointer (or vice versa). Since the pointer was never supposed to be dereferenced in the first place, existing code won't break.
Win32 API does this with just about every structure (if not all)
POINT => *LPPOINT
WNDCLASSEX => *LPWNDCLASSEX
RECT => *LPRECT
PRINT_INFO_2 => *LPPRINT_INFO_2
It's nice how it is consistent, but in my opinion it doesn't add any elegance.
This can help you avoid some errors. For example in following code:
int* pointer1, pointer2;
pointer2 is not an int *, it is simple int. But with typedefs this is not gonna happen:
typedef int* pInt;
pInt pointer1, pointer2;
They are both int * now.
Typedef is used to make code more readable, but making pointer as typedef will increase confusion. Better to avoid typedef pointers.
It (like so many answers) depends.
In C this is very common as you are trying to disguise that an object is a pointer. You are trying to imply that this is the object that all your functions manipulate (we know it is a pointer underneath but it represents the object you are manipulating).
MYDB db = MYDBcreateDB("Plop://djdjdjjdjd");
MYDBDoSomthingWithDB(db,5,6,7);
CallLocalFuc(db); // if db is not a pointer things could be complicated.
MYDBdestroyDB(db);
Underneath MYDB is probably a pointer at some object.
In C++ this is no longer required.
Mainly because we can pass things around by reference and the methods are incorporated into the class declaration.
MyDB db("Plop://djdjdjjdjd");
db.DoSomthingWithDB(5,6,7);
CallLocalFuc(db); // This time we can call be reference.
db.destroyDB(); // Or let the destructor handle it.
Some time ago, i'd have answered "no" to this question. Now, with the rise of smart pointers, pointers are not always defined with a star '*' anymore. So there is nothing obvious about a type being a pointer or not.
So now i'd say : it is fine to typedef pointers, as long as it is made very clear that it is a "pointer type". That means you have to use a prefix/suffix specifically for it. No, "p" is not a sufficient prefix, for instance. I'd probably go with "ptr".
The purpose with typedef is to hide the implementation details, but typedef-ing the pointer property hides too much and makes the code harder to read/understand. So please do not do that.
If you want to hide implementation details (which often is a good thing to do), do not hide the pointer part. Take for instance at the prototype for the standard FILE
interface:
FILE *fopen(const char *filename, const char *mode);
char *fgets(char *s, int size, FILE *stream);
here fopen returns a pointer to some structure FILE
(which you do not know the implementation details for). Maybe FILE
is not such a good example because in this case it could have worked with some pFILE type that hid the fact that it is a pointer.
pFILE fopen(const char *filename, const char *mode);
char *fgets(char *s, int size, pFILE stream);
However, that would only work because you never mess around with the content that is pointed to directly. The moment you typedef some pointer that you some places modify the code becomes very hard to read in my experience.
If you do this, you will be unable to create STL containers of const pSomeStruct since the compiler reads:
list<const pSomeStruct> structs;
as
list<SomeStruct * const> structs;
which is not a legal STL container since the elements are not assignable.
See this question .