Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
January 22, 2007 const methods() | ||||
---|---|---|---|---|
| ||||
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? |
January 22, 2007 Re: const methods() | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mike | 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 :(.
|
January 22, 2007 Re: const methods() | ||||
---|---|---|---|---|
| ||||
Posted in reply to Frits van Bommel | Frits van Bommel wrote: > the way C++ does const sucks :P. Why? -- AKhropov |
January 22, 2007 Re: const methods() | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrey Khropov | 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 :).
|
January 22, 2007 Re: const methods() | ||||
---|---|---|---|---|
| ||||
Posted in reply to Frits van Bommel | So what should I do until an alternative arrives? Just hope that no one messes with my sensitive class objects? |
January 22, 2007 Re: const methods() | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mike | 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
|
Copyright © 1999-2021 by the D Language Foundation