where to place * in pointer type

Pages: 1234
I use int * p;.
It looks good at my eyes, and, I HEARD (Not sure) this is the best way.
But probably there's no difference between any of those "notations".
Choose a single standard and stick with it in your project. It doesn't really matter which one you choose or why it's been chosen as someone else will have the standard imposed on them.
I use int * p;. I want maximum visibility. (But still, don't sweat the small stuff.)
Last edited on
I like int *p
Last edited on
typedef int* int_pointer; ;)
déjà vu

Whenever something can be done in two ways, someone will be confused.
Whenever something is a matter of taste, discussions can drag on forever.

Stick to one pointer per declaration and always initialize variables,
and the source of confusion disappears.
- Stroustrup
@ne555 int_pointer is quite long... i'd use something like pInt.
Also:
1
2
3
typedef int* pInt; // Valid
typedef int * pInt; // Valid
typedef int *pInt; // Also Valid 
And now the discussion goes to where put the asterisk in the typedef, xP.
And now C++11 users chime in with the more human-readable syntax of

1
2
using pInt = int*;  // valid 
using pInt = int *; // also valid 
closed account (z05DSL3A)
I must admit that I like to use int * p;. My thinking; * is a pointer operator not a type, so it does not belong with the type specifier and it is not part of the name.

Then when you get to int X::* ptr... (pointer to a member of X of type int), it is still consistent

@JLBorges

Do not repeat any foolishness after Stroustrup . Have you own head.:)

Consider an example of a local class

1
2
3
4
5
6
7
class A
{
public:
   A( int i = 0 ) : x( i ) {}
private:
   int x;
} a( 10 ), *pa = &a;
Last edited on
@vlad
You keep bringing up the same argument time after time. From this thread it is clear that many people that write int* p; (me included) only do one declaration per line so what you say is never a problem. Please accept that people do things in different ways and it's not necessary bad just because it differs from how you do it.
@Peter87

well and what about this

1
2
3
4
5
6
7
typedef class A
{
public:
   A( int i = 0 ) : x( i ) {}
private:
   int x;
} MyClass, *PMyClass;


and do not forget that such declarations are very many in system headers as for example for Windows API.

It is not a style of thinking. It is incomprehension what is a bad style and what is a good style.

And look at last the definition of the declaration in the C++ standard and what is declarator and declarator-list and what is type specifier.
Last edited on
I would do it this way.
1
2
3
4
5
6
7
8
9
10
class A
{
public:
   A( int i = 0 ) : x( i ) {}
private:
   int x;
};

typedef A MyClass;
typedef A* PMyClass;


You are so narrow minded, it's no point arguing.

The standard use int* p in a number of places.
What about this
typedef class A
{
...
} MyClass,

That's bad style already.

It is incomprehension what is a bad style and what is a good style.

I agree with that part!
Last edited on
@Cubbi
Write a letter to Microsoft about this bad style.::)

First of all classes can have no names so you can not write

typedef A MyClass;
typedef A* PMyClass;

Secomdly it is a bad style because I do not see what is MyClass and what are its members. The name of the type is separated from its definition. So then you meet such declaration you should spend a time to find its definition.:)

All what you are doing is to make the live of programmers harder. :)

Good luck!
Last edited on
vlad from moscow wrote:
Write a letter to Microsoft about this bad style.
and do not forget that such declarations are very many in system headers as for example for Windows API.
The Windows API is a C interface. And in C, structs go in their own namespace. So it is common and convenient to make a typedef, and why you're there, define some related types too. So yes:
1
2
3
typedef struct _OVERLAPPED {
//...
} OVERLAPPED, *LPOVERLAPPED;
is common, but that has nothing to do with C++ and I'd go as far to say you're wrong to do that in C++, it's way beyond bad style.

And what does typedefs have to do with the placement of * in a pointer-to-int declaration?
This is the part that got me wondering...
First of all classes can have no names so you can not write
@kbw
So yes:
typedef struct _OVERLAPPED {
//...
} OVERLAPPED, *LPOVERLAPPED;
is common, but that has nothing to do with C++ and I'd go as far to say you're wrong to do that in C++, it's way beyond bad style.


First of all these words ""t's way beyond bad style" have no any sense. It is only your emotions based on incomprehension of 1) what is bad and what is good styles; 2) C and C++ code can be used in one project 3) what is a declarator in C/C++ and what is a type specifier. That is all.:)


@moorecm
This is the part that got me wondering...


Maybe this is because you do not read the C++ standard?

Last edited on
Pages: 1234