P
paulo
Hi,
I've always been trying to search for the best coding standard.
By now I understand that every project/person has its own favorite way
of laying out their code.
Well, I'm ok with most of it, but there's one part that I think goes
beyond pure coding stile. Or maybe not...
I'm talking about the constant qualifier.
My question can be stated as "where can I read about the best
practices for the const qualifier usage"?
For example. When I learned c++ I was thought to use:
class A {};
class B {
public:
//...
B const& attributeName() const;
void attributeName(B const& b) const;
private:
B attribute_name_;
};
I do think it's a good thing to always make constant something that
shouldn't be modified.
So, whenever that situation comes about I write:
SomeType const t(some_initialization_parameter);
//don't want t to be modified from here on...
Sometimes this is actually complicated, as in for loops: (same thing
for iterators)
for(vector<int>::size_type i(0), end(_vector.size()); i != end; ++i)
//here I could modify both i and end. Not good!
I would like to be able to write
for(vector<int>::size_type i(0), const end(_vector.size()); i != end; +
+i)
//sometimes it would be good to write here
vector<int>::size_type const i' = i;
Well, if I declare/initialize "end" before the loop this problem will
disappear, but
I'll pollute the scope with that name.
As for the i', there's no way to guarantee that i would not be
modified anyway.
Any good/elegant idea on how to do this?
Actually I'm just being a bit psychotic, trying to make the compiler
spot all my possible missuses of the language.
Another thing is the point that I fighting with right now.
Consider the following code:
void procedure(A const& a);
is this preferable to, for example:
void procedure(const A &a) const;
And why?
What about the pointer (or basic type) case. Allot o people will
write:
void someProcedure(const B* pb);
Completely different from:
void someProcedure(B const *const pb);
or
void someProcedure(const B* const pb);
Which is not as pretty but is arguably safer.
I would just like to hear/read some opinions about this subject.
I think it might not be considered very important, apart from this
last example, but if some one cares about it, please I would like to
know your opinion.
Thank you.
I've always been trying to search for the best coding standard.
By now I understand that every project/person has its own favorite way
of laying out their code.
Well, I'm ok with most of it, but there's one part that I think goes
beyond pure coding stile. Or maybe not...
I'm talking about the constant qualifier.
My question can be stated as "where can I read about the best
practices for the const qualifier usage"?
For example. When I learned c++ I was thought to use:
class A {};
class B {
public:
//...
B const& attributeName() const;
void attributeName(B const& b) const;
private:
B attribute_name_;
};
I do think it's a good thing to always make constant something that
shouldn't be modified.
So, whenever that situation comes about I write:
SomeType const t(some_initialization_parameter);
//don't want t to be modified from here on...
Sometimes this is actually complicated, as in for loops: (same thing
for iterators)
for(vector<int>::size_type i(0), end(_vector.size()); i != end; ++i)
//here I could modify both i and end. Not good!
I would like to be able to write
for(vector<int>::size_type i(0), const end(_vector.size()); i != end; +
+i)
//sometimes it would be good to write here
vector<int>::size_type const i' = i;
Well, if I declare/initialize "end" before the loop this problem will
disappear, but
I'll pollute the scope with that name.
As for the i', there's no way to guarantee that i would not be
modified anyway.
Any good/elegant idea on how to do this?
Actually I'm just being a bit psychotic, trying to make the compiler
spot all my possible missuses of the language.
Another thing is the point that I fighting with right now.
Consider the following code:
void procedure(A const& a);
is this preferable to, for example:
void procedure(const A &a) const;
And why?
What about the pointer (or basic type) case. Allot o people will
write:
void someProcedure(const B* pb);
Completely different from:
void someProcedure(B const *const pb);
or
void someProcedure(const B* const pb);
Which is not as pretty but is arguably safer.
I would just like to hear/read some opinions about this subject.
I think it might not be considered very important, apart from this
last example, but if some one cares about it, please I would like to
know your opinion.
Thank you.