Re: const qualifers and references
{ extraneous linebreaks removed. -mod }
<PHeadland@excite.com> wrote in message
news:1160247305.945896.323010@b28g2000cwb.googlegroups.com...
: Consider the following:
:
: int* p = NULL;
: int*& r1 = p;
: const int*& r2 = p;
: const int* const& r3 = p;
:
: MS VS 2003 compiles this just fine, as do various proprietary UNIX
: compilers.
:
: I have been told that g++ 4.0.1 (which I don't have installed here)
: complains about the declaration of r2, but is happy with the other two
: references. Is g++ right to complain?
Yes, absolutely.
: If it is right, why, and why does it accept the declaration of r3?
r2 would cause a loophole in the type system,
as one could then use it as follows:
void f(const int* constInput)
{
r2 = constInput;
*p = 666; // modifying const data at *constInput !!
}
Such a loophole is not created by r3 or r1.
Cheers --Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]