Bug? Const Class Makes Constructor Const
Steven Schveighoffer
schveiguy at gmail.com
Thu Oct 25 20:52:55 UTC 2018
On 10/25/18 4:39 PM, Vijay Nayar wrote:
> I suppose const(Bob) is a type in this case. I guess I'm not sure what
> the purpose of having a const constructor is.
The main purpose is if you have const members that have to be assigned
from const data.
For example:
class C
{
int *ptr;
this(int *p) { ptr = p; }
}
If you want to construct a const(C) with a const(int)*, you need a const
constructor:
this(const(int *)p) const { ptr = p; }
I don't see how else you would do it.
> I see the benefit of
> having const members, to assure that the object is not modifed after
> creation, but when the constructor is const as well, that enforces
> head-const behavior as well, so that whenever a reference variable is
> initialized, that it cannot later reference something else.
Then you want a class that has tail-const data, not a const class.
> Maybe it's just best that I avoid this feature and don't put const in
> front of the class.
Yeah, a const class is existentially unchangable after construction.
-Steve
More information about the Digitalmars-d
mailing list