How can you modify an object without calling member functions?

后端 未结 7 2451
不知归路
不知归路 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:28

    I can think of one way:

    If your class exposes public member variables, you can assign directly to those member variables. For example:

    class A
    {
        public:
            int _my_var;
    ...
    };
    
    int main(int argc, char** argv)
    {
        A *a = new C();
        a->_my_var = 10;
    }
    

    This is not a good programming style though - exposing a member variable as public isn't something I would advocate or even suggest.

    Also, if you can do something really weird, such as directly writing some address in memory, an offset from the pointer to the class object - but why would you do that?

提交回复
热议问题