Value representation of non-trivially copyable types

前端 未结 1 1386
梦如初夏
梦如初夏 2020-12-19 13:18

I\'m intrigued by the following paragraph from the standard (§3.9/4 of ISO/IEC 14882:2011(E)):

The object representation of an objec

相关标签:
1条回答
  • 2020-12-19 14:06

    The standard example is a class that manages a resource:

    struct Foo
    {
        Bar * p;
    
        Foo() : p(new Bar) { }
        ~Foo() { delete p; }
    
        // copy, assign
    };
    

    An object of type Foo has a value, but that value is not copyable by copying the object representation (which is just the value of p in this case). Copying an object of type Foo requires copying the se­man­tics of the class, which say "an object owns the pointee". A suitable copy thus requires an appropriate, user-defined copy constructor:

    Foo::Foo(Foo const & rhs) : p(new Bar(*rhs.p)) { }
    

    Now the object representation of an object of type Foo is different from the object representation of a copy of such an object, although they have the same value.

    By contrast, the value of an int is the same as that of another int as soon as the object representations coincide. (This is a sufficient, though not necessary, condition, due to padding.)

    0 讨论(0)
提交回复
热议问题