0

Note: I am using the g++ compiler (which is I hear is pretty good and supposed to be pretty close to the standard).

Not trying to start a grammar war, but just a random question... What is the ideal way to declare a pointer?

int* pI = 4;
int *pI = 4;

(or my favorite, I know it's non-pretty, but I like it):

int*pI = 4;

Same question stands for references:

int& rI = 4;
int &rI = 4;

or

int&rI = 4;

Maybe there is no right answer. Similarly, should I care whether a constant integer is declared as:

const int I = 4;

or

int const I = 4;

I'm fine with not caring...

I do like the way a const function is declared by having a const after the last parenthesis.

And I believe a constant function has a distinct function signature than the similar non-const function (i.e. the const-nesss is part of the function signature, unlike most sources that say it just depends on the arguments type and the return type).

Is this right? Should I care?

Jimmy
  • 4,419
  • 6
  • 21
  • 30

4 Answers4

6

I prefer

int* a;
int& b;

for the following reason: the type is int* and not int. For me - the type belongs together and needs to stand separate from the name. I know this introduces some problems with

int* a, b;

but that's why I don't declare to variables in one line.

Other than that - like VJo said: stick to the coding standard. If everyone around you does it one way, don't do it the other.

Tobias Langner
  • 10,634
  • 6
  • 46
  • 76
4

Coding standards might tell you how to declare or define your variables. Other then that, use whatever suits you better.

BЈовић
  • 62,405
  • 41
  • 173
  • 273
4

And I believe a constant function has a distinct function signature than the similar non-const function (i.e. the const-nesss is part of the function signature, unlike most sources that say it just depends on the arguments type and the return type).

A constant function has a keyword const which is appended at the end of it.

int doSomething() const;

It means that the function will not alter the state(members) of the class.

Mentioning const on an argument implies that the function will not alter the state of that variable(param in below example) being passed to the function.

int doSomething(const int param);

Mentioning const before the return type applies to the type being returned by the function.

const int doSomething();

Implies the function returns a const integer value.

So yes your understanding is correct. And there is no other way to declare a function const except putting a const after the last paranthesis. Also, note that const member function can be only called on a const object, while a non const member function can be called by const as well as non const objects of that class.

As far as the way of declaring, Each organization have their own coding guidelines and you should stick to that, Yes there is no distinct advantage of using those contructs you mentioned in either way with respect to compiler optimization or treatment. Just follow what you like or what your organzation wants you to follow.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • The `const` in `const int doSomething();` is ignored by the compiler. [There is no such thing](http://stackoverflow.com/questions/2169932/) as a scalar const rvalue. – fredoverflow Jul 26 '11 at 06:28
2

Bjarne Stroustrup uses:

int* a;

so, that's good enough for me.

Dave
  • 10,964
  • 3
  • 32
  • 54