Constant value changing

Rubik

New Member
#1
Suppose I have a struct definition:
Mã:
struct thing
{
    thing* x;
    int z;

    thing() : x(this), z(0) {}
    void foo() const
    {
        this->x->z++;
    }
};
Note that I create a mutable pointer to myself (evil laugh)

And then I can use this later like this:
Mã:
int main()
{
    const thing c;
    c.foo();
    assert(c.z == 1);
    c.foo();
    assert(c.z == 2);
    return c.z;
}
And as you can see it seems that I can change a constant value......is this UB?
 

Admin

Administrator
Thành viên BQT
#2
[dcl.type.cv]p4:
Except that any class member declared mutable ([dcl.stc]) can be modified, any attempt to modify ([expr.ass], [expr.post.incr], [expr.pre.incr]) a const object ([basic.type.qualifier]) during its lifetime ([basic.life]) results in undefined behavior.
[basic.type.qualifier]p1:
A const object is an object of type const T or a non-mutable subobject of such an object.
c.z is a const object, because it is a non-mutable subobject of c. Your code attempts to modify it during its lifetime. It follows that the code has undefined behavior.
 

Từ khóa phổ biến

You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an alternative browser.

Top