Indirection operator placement

M

Marty

I am wondering what is the difference in the placement between the 2
uses of the indirection operator? Or is there a difference?

AcDbBlockTable *pBlockTable = NULL;

AcDbDatabase* pDB = acdbHostApplicationServices()->workingDatabase();
 
F

Frederick Gotham

Marty posted:
I am wondering what is the difference in the placement between the 2
uses of the indirection operator? Or is there a difference?

AcDbBlockTable *pBlockTable = NULL;

AcDbDatabase* pDB = acdbHostApplicationServices()->workingDatabase();


A C++ compiler ignores white space, so it sees all of the following
identically:

int*p=0;
int *p=0;
int* p = 0;
int * p = 0 ;

I think it's intuitive, however, to write it as follows:

int *p = 0;

, because it conveys that the asterisk is a part of the object's name --
which is how things actually work:

int *p, *q, r[5], (*s)[5], *t[5], Func(), *Func2(), (*Func3())[5];

p is a pointer.
q is a pointer.
r is an array of 5.
s is a pointer to an array of 5.
t is an array of 5 pointers.
Func is a function which returns an int.
Func2 is a function which returns a pointer to an int.
Func3 is a function which returns a pointer to an array of 5.
 
H

Howard

Marty said:
I am wondering what is the difference in the placement between the 2
uses of the indirection operator? Or is there a difference?

AcDbBlockTable *pBlockTable = NULL;

AcDbDatabase* pDB = acdbHostApplicationServices()->workingDatabase();

I assume you're talking about the "*" symbols in the code above? That's not
an operator. It specifies that the object being declared is a pointer to
the type specified on the left of the * symbol. The spacing is irrelevant.
The following are all the same:

int* pInt;
int *pInt;
int * pInt;
int * pInt;

All those line declare a variable named pInt whose type is "pointer-to-int".

Some people prefer to put the * immediately after the type, others prefer to
put it immediately before the identifier, and still others prefer to put
spaces on both sides.

The compiler doesn't care one way or the other. :)

-Howard
 
M

Marcus Kwok

Marty said:
I am wondering what is the difference in the placement between the 2
uses of the indirection operator? Or is there a difference?

AcDbBlockTable *pBlockTable = NULL;

AcDbDatabase* pDB = acdbHostApplicationServices()->workingDatabase();

This is answered on Stroustrup's FAQ:
http://www.research.att.com/~bs/bs_faq2.html#whitespace

In short, they are the same. It's merely a matter of style which form
you use. However, as noted on the referenced page, the confusion comes
when someone tries to declare multiple pointers with a single
declaration:

int* p1; // pointer to int
int *p2; // also pointer to int

int* p3, p4; // p3 is a pointer to int, p4 is a regular int
int* p5, *p6; // both p5 and p6 are pointers to int

Since I prefer one declaration per line, this doesn't affect me, so I
use the (int* p;) form instead of the (int *p;) form, for the reason
given on the page: (int* p;) emphasizes the type (p is a pointer to
int), versus (int *p;) which emphasizes the syntax (*p is an int).
 
M

Marty

Thanks all!
I am new to C++ (if you can't tell) but you confirmed what I thought
was the answer.
When I started looking through the example project, they used it as I
had shown.
It started to get me to double guess myself.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top