Re: Const constructor
Francis Glassborow wrote:
Dragan Milenkovic wrote:
"A" is what OP was trying to design...
Fabio wrote:
struct A {
A( int* a) : x(a) {}
const A( const int* a) : x( const_cast<int*>(a) ) {}
const int& get() const { return *x; }
int& get() { return *x; }
int *x;
};
The problem is "A a2(a1)" creates a mutable object from a const one,
which allows "x" to be written to. But see below first...
Oh, my mistake. I thought we were talking about well designed classes. I
would need a really good justification for allowing a class to copy a
pointer by value. I really do not want classes sharing members that way.
Indeed having a pointer member is one of the main indicators that your
class needs a user written copy ctor.
Actually, we _were_ talking about well designed classes. But no one that
is advocating const-ctors has provided an example of such a well defined
class. I'm very interested to seeing one, or a reference to a document
or a discussion. The link to an earlier proposal didn't provide
all the answers. Namely, operator= is not mentioned anywhere!?!
The only designs that were mentioned in this thread are Fabio's class A
and iterators. Both have a pointer-like semantics, so this is why
I fail to see the mentioned "necessity" and how would it all work.
In fact, almost all designs where I needed a functionality like this
fall under either pointer-like or wrapper classes.
Thanks for any further explanation.
--
Dragan
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]