cannot modify struct with immutable members

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Jan 3 01:00:58 PST 2015


On 01/02/2015 09:07 PM, ketmar via Digitalmars-d-learn wrote:

 > structure instance with const fields can be initialized only once, upon
 > creation. so did `Test myTest1;` -- you initialized `myTest1` with
 > default values. you can't reinitialize it later.
 >
 > in C++ constness on member doesn't impose such restrictions.

C++ has the same restriction: const members make objects unassignable:

struct S
{
     const int i;

     S()
         : i()
     {}
};

int main()
{
     S a;
     S b;
     a = b;
}

error: non-static const member ‘const int S::i’, can't use default 
assignment operator

Ali



More information about the Digitalmars-d-learn mailing list