Can a heap-allocated object be const in C++?

后端 未结 6 1665
轮回少年
轮回少年 2020-12-08 11:46

In C++ a stack-allocated object can be declared const:

const Class object;

after that trying to call a non-const method on suc

相关标签:
6条回答
  • 2020-12-08 11:54

    const_cast can cause UB when the object is actually read-only (for example, the compiler can create such objects when you use hard coded strings in your code, by placing them in certain memory areas that are read only) for some reason. This will not happen with heap allocated objects, no matter how you keep their reference (const pointer, const reference, whatever).

    0 讨论(0)
  • 2020-12-08 11:57

    Don't forget mutable members

    It won't be undefinied behaviour if the NonConstMethod only modifies mutable qualified members (see 7.1.5.1 (4)) of a const qualified class. Yes, otherwise it's undefined behaviour.

    const A* p = new(const A);
    A *q = const_cast<A*>(p);
    q->NonConstMethodThatModifiesMembers();             // undefined behaviour!
    q->NonConstMethodThatOnlyModifiesMutableMembers();  // defined behaviour!
    
    0 讨论(0)
  • 2020-12-08 12:06

    Obviously:

    struct Foo {
      const int Bar;
      Foo() : Bar(42) { }
    };
    
    Foo* foo = new Foo;
    const_cast<int&>(foo->Bar); // don't do this.
    
    0 讨论(0)
  • 2020-12-08 12:10

    In your heap example, new returns a pointer to non-const. The fact that you've stored it in a pointer to const (and then const_casted it back to a pointer to non-const) doesn't change the fact that the object itself is not const in the same way as the stack-allocated one is.

    However, you can create a const object on the heap:

    const Class* object = new const Class();
    

    In such a case, casting to a pointer to non-const and calling a non-const method would be the same situation as the const stack-allocated object.

    (The idea of creating a const object on the heap was new to me, I had never seen that before. Thanks to Charles Bailey.)

    0 讨论(0)
  • 2020-12-08 12:10

    Yes, a heap-allocated object can be const. Consider this excerpt from the example in 7.1.5.1/5:

    const int* ciq = new const int (3);    // initialized as required
    int* iq = const_cast<int*>(ciq);       // cast required
    *iq = 4;                               // undefined: modifies a const object
    

    The example you gave in the question is fine because you're not asking new to make a const object; you're just storing the result in a pointer-to-const.

    0 讨论(0)
  • 2020-12-08 12:16

    Yes. It's legal to construct and destroy a const heap object. As with other const objects, the results of manipulating it as a non-const object (e.g. through a const_cast of a pointer or reference) causes undefined behaviour.

    struct C
    {
            C();
            ~C();
    };
    
    int main()
    {
            const C* const p = new const C;
    
            C* const q = const_cast<C*>(p); // OK, but writes through q cause UB
    
            // ...
    
            delete p; // valid, it doesn't matter that p and *p are const
    
            return 0;
    }
    
    0 讨论(0)
提交回复
热议问题