Answer the question
In order to leave comments, you need to log in
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
The first option semantically really denotes a separate type. The problem is this:
Type a, b,c;
Type* pA, pB, pC;
Type a, b,c;
Type *pA, pB, *pC;
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){}
I like the first option
(the same as you)
besides, this is our corporate standard.
I use the first option, simply because if you suddenly have to write somewhere:
void func( char* &pointer )
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; ... }
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question