B
B
BiFive2010-12-22 10:45:02
C++ / C#
BiFive, 2010-12-22 10:45:02

Where is the asterisk?

One question intrigued me. Where is it better to put * when declaring a pointer to an object: next to the class name or next to the variable name?
The question is purely philosophical. The most obvious answer is to whom it is more convenient. I asked my colleagues and it turned out that I'm the only one who puts * next to the class name.
Decided to look into this issue. I turned to the original source (Bjorn Stroustrup. The C++ programming language).
char * p; // указатель на символ
That is, in general, the third option.
So I decided to look at the standard . I didn't specifically find about pointers, only a piece of code where they are declared:
Tmp* p = new Tmp(a[i]*b[i]);
Before asking your opinion, I will express my thoughts and thoughts. Everything that follows is just my opinion.
First option Type* obj
I myself use this option, but I started using it even before I thought about the meaning of the location *. In this case, the variable is treated as a variable of type "pointer to an object of type Type". Therefore, a pointer is a separate type.
The second option Type *obj
Well, everything is simple here, it's just a pointer, that is, a number with an address where you can find an object of type Type.
Actually the question itself: How do you declare pointers and is there a semantic load in this or is it just a matter of habit?

Answer the question

In order to leave comments, you need to log in

6 answer(s)
O
Ogra, 2010-12-22
@Ogra

The first option semantically really denotes a separate type. The problem is this:

Type a, b,c;
Type* pA, pB, pC;

In this code, everything looks like a, b, c are objects, and pA, pB, pC are pointers to objects. In fact, only pA is a pointer.
The second option is more accurate:
Type a, b,c;
Type *pA, pB, *pC;

O
Ogra, 2010-12-22
@Ogra

Looked at Firefox sources.
Found in one file:

  // Первый вариант встречается, но редко:
  JSObject* obj = nativeWrapperCache->GetWrapper();

  // Второй встречается существенно чаще 
  const nsIID *iid = nsnull;

  JSContext *cx = nsnull;
  nsGlobalWindow *win = nsGlobalWindow::FromWrapper(wrapper);

  // Функция:
  NS_IMETHODIMP nsWindowSH::GetProperty(nsIXPConnectWrappedNative *wrapper, JSContext *cx, JSObject *obj, jsid id, jsval *vp, PRBool *_retval){}

5
57DeD, 2010-12-22
@57DeD

I like the first option
(the same as you)
besides, this is our corporate standard.

T
Tujh, 2010-12-22
@Tujh

I use the first option, simply because if you suddenly have to write somewhere:

void func( char* &pointer )

then it will look alien among the code written in the second style.

M
MikhailEdoshin, 2010-12-22
@MikhailEdoshin

I use the second option if I have a variable name (when declaring) and the first option if there is no name (for example, in a function).

char*
foo(int*);

char*
foo(
     int*bar)
{
    char*baz1,
         *base2;
   ...
}

W
wholeman, 2010-12-22
@wholeman

The first option: for me, the asterisk is part of the type name.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question