C++ Pointer: changing the contents without changing the address?

前端 未结 6 1821
悲&欢浪女
悲&欢浪女 2020-12-31 05:11
MyCustomObject * object=new MyCustomObject();

Assume the object pointer is used by many of my classes, but all of a sudden I want to change the con

6条回答
  •  星月不相逢
    2020-12-31 05:30

    The pointer IS the object, so if you do new Object() you create a new one, but the functions with the prevoius pointer will not see it. If you want to change the content, you can do this, because all other objects which know the pointer will reference the values that your object contains. It's a bit similar to a global variable. So you can pass around the pointer and all functions will see your changes as soon as they acces the object.

提交回复
热议问题