How can you modify an object without calling member functions?

后端 未结 7 2470
不知归路
不知归路 2021-02-13 15:41

At 3.10/10, the standard says:

An lvalue for an object is necessary in order to modify the object except that an rvalue of class type can also be used to

7条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-13 16:11

    Modifying a temporary and not through an lvalue to that temporary:

    #include 
    
    class standard_layout {
        standard_layout();
        int stuff;
    };
    
    standard_layout* global;
    
    standard_layout::standard_layout()
    { global = this; }
    
    void
    modify(int)
    {
        std::memset(global, 0, sizeof *global);
    }
    
    int
    main()
    {
        modify( (standard_layout {}, 0) );
    }
    

    I don't think it's correct to assume that rvalues of class types are non-modifiable. I now understand that paragraph as 'for non-class types, an lvalue for an object is needed in order to modify that object'.

提交回复
热议问题