Re: Reference to void
James Kanze wrote:
Except that C++ isn't English. Otherwise, a pointer to int
would be declared "*int p;". And const(ant) pointer to int
"const *int p;". The whole point of the argument is that in
most cases, you don't have a choice; the modifier must come
after what it modifies. So there is a strong argument to be
coherent.
So a "const(ant) int" is simply more idiomatic a phrase than "int
const(ant)".
In English. What's idiomatic in C++ is what is actually used in
C++. In this particular case, I'd say that both are idiomatic:
historically, "const int* p;" was by far the most prevelant, but
today, I'd say that both are wide spread.
The real argument for post-positionning the const, of course, is
related to typedefs:
typedef int* PtrInt ;
const PtrInt pi1 ; // == int *const !!!
This sort of thing seems to create so much confusion in the
minds of beginners that it's better avoided.
And to intermediate (and even fairly advanced) programmers they get
confused in the world of smart pointers.
const T* p; // pointer to const T
const shared_ptr< T > p; // not quite what they wanted...
A typical smart pointer implementer might try this:
template < typename T >
class SmartPointer
{
T* ptr;
public:
T& operator*() { return *ptr; }
const T& operator *() const { return *ptr; }
// plus other stuff
};
void func( const SmartPointer< T > & p ) // surely the can't modify the
T
{
SmartPointer<T> dup( p ); // assuming we have a copy-constructor
T & t = *dup; // hey dup is non-const
t.non_const_method(); // oh yes I can modify it.
}
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]