Pointer symbol position and constant member functions
-
I got two questions. 1 - Which is the preferred way of using the pointer symbol(*). Is it along with the type or with the identifier?
int* intPtr
OR
int *intPtr;
IMO, the second one is more clear when we declare something like this.
int *intPtr1,intPtr2;
2 - Is it a good practice to append
const
with member function that doesn't modify any member variables?int foo::sampleMethod() const{
}I understand why const member functions exists. But if we are not planning to make the class object as constant, do we need this kind of declarations? Any help would be great.
-
I got two questions. 1 - Which is the preferred way of using the pointer symbol(*). Is it along with the type or with the identifier?
int* intPtr
OR
int *intPtr;
IMO, the second one is more clear when we declare something like this.
int *intPtr1,intPtr2;
2 - Is it a good practice to append
const
with member function that doesn't modify any member variables?int foo::sampleMethod() const{
}I understand why const member functions exists. But if we are not planning to make the class object as constant, do we need this kind of declarations? Any help would be great.
I personally prefer the
int* intPtr;
But I declare a single variable in a line. This way I can add a comment for each variable I use. I do add const to each member function that doesn't modify member variables. The problem is you can almost never predict how the class is going to be used at a later time. Once I had to add const to loads of member function for about 10-20 classes. Also, I am not sure, but in certain cases adding const might help compiler to produce more optimized code. -Saurabh -
I got two questions. 1 - Which is the preferred way of using the pointer symbol(*). Is it along with the type or with the identifier?
int* intPtr
OR
int *intPtr;
IMO, the second one is more clear when we declare something like this.
int *intPtr1,intPtr2;
2 - Is it a good practice to append
const
with member function that doesn't modify any member variables?int foo::sampleMethod() const{
}I understand why const member functions exists. But if we are not planning to make the class object as constant, do we need this kind of declarations? Any help would be great.
you have an interview, don't you ? :)
Christian Flutcher wrote:
1 - Which is the preferred way of using the pointer symbol ?
for me, I like to declare every variable separatedly, and I don't like to mix declarations of different types. this way, I prefer using
int* pi;
syntax because I see immediately that pi is of type int*.Christian Flutcher wrote:
2 - Is it a good practice to append const with member function that doesn't modify any member variables?
definitely, yes ! because the one who write the class is not always the one who will use it, a class definition has to be clear, and has to mean what the class is designed for. When you have an accessor which just does a "get", the it's obviously not modifying the object, and should be declared as const.
Christian Flutcher wrote:
I understand why const member functions exists. But if we are not planning to make the class object as constant,
humm, it seems to me that you don't fully understand the thing. making a function member constant doesn't mean every function members have to be consts, and it doesn't mean either that the object will be used as a constant. If you'd like to say that the object is used as a constant, then you would have to do like this :
class foo { ... };
const foo f(/* some initialization parameters */);
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
-
I got two questions. 1 - Which is the preferred way of using the pointer symbol(*). Is it along with the type or with the identifier?
int* intPtr
OR
int *intPtr;
IMO, the second one is more clear when we declare something like this.
int *intPtr1,intPtr2;
2 - Is it a good practice to append
const
with member function that doesn't modify any member variables?int foo::sampleMethod() const{
}I understand why const member functions exists. But if we are not planning to make the class object as constant, do we need this kind of declarations? Any help would be great.
1/ I normally do:
int *pInt;
2/ the question you ask is why I never mix those. And if the variable it at all significant, it gets its own line. I use long variable names - thanks to intellitext they're just as easy to type, and a lot easier to read. Only loop vars get bunched together.
int i,j,k;
3/ I try to use const on member functions that *shouldn't* modify the state, not whether I think I might do so. It gives me the freedom to change my mind in one direction, and makes it harder to code mistakes in the other. Iain. -
you have an interview, don't you ? :)
Christian Flutcher wrote:
1 - Which is the preferred way of using the pointer symbol ?
for me, I like to declare every variable separatedly, and I don't like to mix declarations of different types. this way, I prefer using
int* pi;
syntax because I see immediately that pi is of type int*.Christian Flutcher wrote:
2 - Is it a good practice to append const with member function that doesn't modify any member variables?
definitely, yes ! because the one who write the class is not always the one who will use it, a class definition has to be clear, and has to mean what the class is designed for. When you have an accessor which just does a "get", the it's obviously not modifying the object, and should be declared as const.
Christian Flutcher wrote:
I understand why const member functions exists. But if we are not planning to make the class object as constant,
humm, it seems to me that you don't fully understand the thing. making a function member constant doesn't mean every function members have to be consts, and it doesn't mean either that the object will be used as a constant. If you'd like to say that the object is used as a constant, then you would have to do like this :
class foo { ... };
const foo f(/* some initialization parameters */);
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
toxcct wrote:
you have an interview, don't you ?
:) No I don't have. I just finished reading "Thinking in C++" and about to start a project in C++. So thought of getting some expert advice on those points. Thanks for you help. It was really helpful.
-
1/ I normally do:
int *pInt;
2/ the question you ask is why I never mix those. And if the variable it at all significant, it gets its own line. I use long variable names - thanks to intellitext they're just as easy to type, and a lot easier to read. Only loop vars get bunched together.
int i,j,k;
3/ I try to use const on member functions that *shouldn't* modify the state, not whether I think I might do so. It gives me the freedom to change my mind in one direction, and makes it harder to code mistakes in the other. Iain.Perfect ! Thanks for the help Iain.
-
I personally prefer the
int* intPtr;
But I declare a single variable in a line. This way I can add a comment for each variable I use. I do add const to each member function that doesn't modify member variables. The problem is you can almost never predict how the class is going to be used at a later time. Once I had to add const to loads of member function for about 10-20 classes. Also, I am not sure, but in certain cases adding const might help compiler to produce more optimized code. -SaurabhSaurabh.Garg wrote:
But I declare a single variable in a line. This way I can add a comment for each variable I use.
Saurabh.Garg wrote:
The problem is you can almost never predict how the class is going to be used at a later time.
Good points.
Saurabh.Garg wrote:
I am not sure, but in certain cases adding const might help compiler to produce more optimized code.
Do you have any articles/books which explains this? Thanks for your help