Topic: Const references (not references to const) ??
Author: kanze@gabi-soft.fr (J. Kanze)
Date: 1997/12/15 Raw View
Oleg Zabluda <zabluda@math.psu.edu> writes:
|> class A{
|> int& a;
|> public:
|> A() : a(*new int(42)) {}
|> };
|>
|> const A b;
|>
|> OK, what is the type of b.a here? I always naively thought that
|> the type would be "const int&". However, recent discussions
|> prompted me to realize that it doesn't follow from anything.
|> At least not that I know of. Formal rules seem to say that the
|> type is "int& const". Now, that's nonsense.
Written that way, yes. References are always const. The type is int&,
simply. (Just as the type of an int*const would be int*const, and not
int*const const.)
--
James Kanze +33 (0)1 39 23 84 71 mailto: kanze@gabi-soft.fr
GABI Software, 22 rue Jacques-Lemercier, 78000 Versailles, France
Conseils en informatique orient e objet --
-- Beratung in objektorientierter Datenverarbeitung
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: David R Tribble <david.tribble@central.beasys.com>
Date: 1997/12/16 Raw View
References: <t7hg8ew70d.fsf@calumny.jyacc.com>
Oleg Zabluda <zabluda@math.psu.edu> wrote:
> class A{
> int& a;
> public:
> A() : a(*new int(42)) {}
> };
>
> const A b;
>
> OK, what is the type of b.a here?
The type is 'int &', which is identical to 'int & const'. The use
of b.a in any expression then becomes 'int', since it's a reference.
References are, for all practical purposes, constant; i.e., type
'int &' and 'int & const' can be considered equivalent for the simple
reason that references can't be modified (only the object they refer to
can be modified). The phrase "const reference" usually means "reference
to a const object", such as a type like 'const int &'. Member b.a is
clearly (non-const) 'int &'.
[This next comment is off-topic.]
IMHO, the use of 'int const &' is more confusing than the tried and true
'const int &'. Placing storage-class specifiers willy-nilly in any order
is deprecated practice in C, and may be in C++ (?); the arbitrary
placement of cv-qualifiers could become deprecated next.
[This next comment is also off-topic.]
I hope your class has a destructor that does a delete, or else you've
got a class with a memory leak:
A::~A() { delete &a, a = NULL; }
-- David R. Tribble, david.tribble@noSPAM.central.beasys.com --
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: Oleg Zabluda <zabluda@math.psu.edu>
Date: 1997/12/11 Raw View
class A{
int& a;
public:
A() : a(*new int(42)) {}
};
const A b;
OK, what is the type of b.a here? I always naively thought that
the type would be "const int&". However, recent discussions
prompted me to realize that it doesn't follow from anything.
At least not that I know of. Formal rules seem to say that the
type is "int& const". Now, that's nonsense.
Ok, reading 8.3.3 -- References, paragraph 1, we get:
... Cv-qualified references are ill-formed except when the
cv-qualifiers are introduced through the use of typedef or a
template type argument, in which case cv-qualifiers are ignored ...
Hmm... The case of a const object of a class with a reference member
is not mentioned. Should I conclude that the code above is ill-formed
(blech) or that this is a bug in the [almost] ISO C++, or that I am simply
missing something?
Oleg.
--
Life is a sexually transmitted, 100% lethal disease.
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: "Bradd W. Szonye" <bradds@concentric.net>
Date: 1997/12/13 Raw View
Oleg Zabluda wrote in message <66ntqe$6k2@marianna.psu.edu>...
>class A{
> int& a;
> public:
> A() : a(*new int(42)) {}
>};
>
>const A b;
>
>OK, what is the type of b.a here?
My understanding has been that, given:
class A {
int & a1;
int * a2;
int a3[DIM];
/* ... */
};
A const b;
... then the members of A work like the appropriate typedefs:
int & const a1 <=> int & a1
int * const a2 <=> int * const a2
int a3[DIM] const <=> int const a3[DIM]
In other words, const arrays have const elements, but other indirect
referents remain mutable. The compilers I've used seem to agree; in fact,
using references was one work-around I often used to emulate "mutable"
members.
---
Bradd W. Szonye
bradds@concentric.net
http://www.concentric.net/~Bradds
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: Valentin Bonnard <bonnardv@pratique.fr>
Date: 1997/12/13 Raw View
Oleg Zabluda wrote:
>
> class A{
> int& a;
> public:
> A() : a(*new int(42)) {}
> };
>
> const A b;
I think it's very clear that the int isn't qualified here.
> Formal rules seem to say that the
> type is "int& const". Now, that's nonsense.
>
> Ok, reading 8.3.3 -- References, paragraph 1, we get:
>
> ... Cv-qualified references are ill-formed except when the
> cv-qualifiers are introduced through the use of typedef or a
> template type argument, in which case cv-qualifiers are ignored ...
I don't know if it says that, but the intent is that const
qualifiyng a class is equivalent to the typedef case.
--
Valentin Bonnard mailto:bonnardv@pratique.fr
info about C++/a propos du C++: http://www.pratique.fr/~bonnardv/
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: Hyman Rosen <uunet!jyacc!hymie@ncar.UCAR.EDU>
Date: 1997/12/13 Raw View
Oleg Zabluda <zabluda@math.psu.edu> writes:
> class A{
> int& a;
> public:
> A() : a(*new int(42)) {}
> };
>
> const A b;
>
> OK, what is the type of b.a here?
The type is 'int'. If you want the referant of a to be immutable,
decalre the member as 'const int &a'. Note that this is not much
different than
const class A { int *a; public: A() : a(new int (42)) { } } b;
*b.a = 7 * 9;
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]