Where is the asterisk?


Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
3 views
I was interested in one question. Where better to put the * when declaring a pointer to an object: class name or variable name?

The question is purely "philosophical". The most obvious answer is whatever you like. I asked around my colleagues I found out I put one * class name.

Decided to look into this matter. Turned to the original source (Bjarne Stroustrup. The C programming language).

char * p; // pointer to symbol


That is generally a third option.


So decided to look in the standard. About signs not specifically found was a piece of code where they are declared:

Tmp* p = new Tmp(a[i]*b[i]);


Before asking your opinion, I will Express their views and reflections. Everything that will be written next is just my opinion.

The first variant Type* obj


I myself use this, but I started to use it even before thinking about the meaning of at *. In this case the variable is considered as a variable of type "pointer to object type". Therefore a pointer is a separate type.

The second variant Type *obj


Well it's simple, it's just a pointer, that is, the number with the address where you can find the object of type type.


Actually a question: How do you declare pointers and is there a meaning or is it just a matter of habit?
by | 3 views

6 Answers

0 like 0 dislike
The first option is semantically really refers to a separate type. Here is the problem:
\r
Type a, b,c; Type* pA, pB, pC; 

In this code, it looks like a, b, c — objects, and pA, pB, pC are pointers to objects. In fact, the pointer is only pA
\r
The entry for the second option is more accurate:
\r
Type a, b,c; Type *pA, pB, *pC; 
by
0 like 0 dislike
Looked at the source code of Firefox.
In one file was found:
\r
 // First option found, but rarely: JSObject* obj = nativeWrapperCache->GetWrapper(); // The second occurs much more often const nsIID *iid = nsnull; JSContext *cx = nsnull; nsGlobalWindow *win = nsGlobalWindow::FromWrapper(wrapper); // Function: NS_IMETHODIMP nsWindowSH::GetProperty(nsIXPConnectWrappedNative *wrapper, JSContext *cx, JSObject *obj, jsid id, jsval *vp, PRBool *_retval){} 
by
0 like 0 dislike
The first option
I like (what You)
\r
in addition, we have a corporate standard.
by
0 like 0 dislike
Use the first option, simply because if you suddenly have to write down somewhere:
\r
void func( char* &pointer )

it will look alien among the code written in the second style.
by
0 like 0 dislike
I use the second option if I have the name of the variable (at Declaration) and the first option, if no name (e.g., functions).
\r
\r
char* foo(int*); char* foo( int *bar) { char *baz1, *baz2; ... }
by
0 like 0 dislike
First option: I have the asterisk — part of the name type.
by

Related questions

0 like 0 dislike
2 answers
0 like 0 dislike
2 answers
0 like 0 dislike
1 answer
asked Sep 8, 2019 by vladislav9619996
0 like 0 dislike
1 answer
asked Aug 3, 2019 by vladislav9619996
110,608 questions
257,186 answers
0 comments
27,898 users