Topic: members of const objects


Author: isi@panix.com (Integrated Software Inc.)
Date: 1995/09/23
Raw View
Given:
class A {
public:
 char* p;
};

const A& a = someA;

Is the type of a.p: const char* const or char* const?

Section 9 [class] of the April working paper does not seem to say anthing
about members of constant objects, except in passing in 9.4.2.
3.9.3.3 says:
> Each non-function, non-static, non-mutable member of a const-qualified
> class object is const-qualified, each non-function, non-static member of
> a volatile-qualified class object is volatile-qualified and similarly for
> members of a const-volatile class. See 8.3.5 and 9.4.2 regarding
> cv-qualified function types.

It does not say what const-qualified means for a T*, or T&.
Can someone tell me?


My compiler says that a.p is a char* const.
This opens up an ugly hole in const-ness:
 class D;
 class B {
 public:
  D* d;
  B(D* rd) : d(rd) { }
 };
 class D: public B {
 public:
  int i;
  D(int x) : B(this), i(x) { }
 };
 void f(const B& b) { b.d->i = 0; }

 int main()
 {
  D d(10);
  cout << d.i << endl;
  f(rb);
  cout << d.i << endl;
  return 0;
 }
Here we pass d to f() as a const B&, expecting that the const-ness will
protect d.

Thanks for any clarifications.
Atman Binstock
Integrated Software Inc.
isi@panix.com
---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]