www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - const methods()

reply Mike <mike mike.com> writes:
D doesnt seem to have any way of protecting a class object from being changed
by non-const functions. For example in C++ I can declare a class reference as
const, so no one can call any methods on that reference that would change the
contents of the object. Why can you not do this in D? What is the alternative
line of thought to this?
Jan 22 2007
parent reply Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
Mike wrote:
 D doesnt seem to have any way of protecting a class object from being changed
 by non-const functions. For example in C++ I can declare a class reference as
 const, so no one can call any methods on that reference that would change the
 contents of the object. Why can you not do this in D? What is the alternative
 line of thought to this?

There's not really an alternative line of thought involved, except in the sense that the way C++ does const sucks :P. Active work is currently being done to design a way to do const (and some other things) that is hopefully better. Until then, we'll have to do without :(.
Jan 22 2007
next sibling parent reply "Andrey Khropov" <andkhropov_nosp m_mtu-net.ru> writes:
Frits van Bommel wrote:

 the way C++ does const sucks :P.

Why? -- AKhropov
Jan 22 2007
parent Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
Andrey Khropov wrote:
 Frits van Bommel wrote:
 
 the way C++ does const sucks :P.

Why?

Well, that seems to be Walters motivation, at least. Though I might have overstated it a bit for effect :).
Jan 22 2007
prev sibling parent reply Mike <mike mike.com> writes:
So what should I do until an alternative arrives? Just hope that no one messes
with my sensitive class objects?
Jan 22 2007
parent BCS <ao pathlink.com> writes:
Reply to mike,

 So what should I do until an alternative arrives? Just hope that no
 one messes with my sensitive class objects?
 

Its really hacky but you might be able to make something like this work interface C { int nonConst(); } private ConstC : C { int nonConst(){assert(0);} } private NonConstC : C { int nonConst() {/*go*/} } having a const and non const view might be really messy (nested non private classes maybe?) On second thought, you'd better not
Jan 22 2007