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

前端 未结 6 1820
悲&欢浪女
悲&欢浪女 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:23

    Everytime you create a new object via the new operator the system looks for a good address for it on the heap. You cannot instruct it to give you a certain address, even if you know it's free and usable.

    The only thing you can do is change properties of the object at a certain address, like the number stored in an int variable.

    You're right, the operator new returns a new address, or let's say the address the system decided.

提交回复
热议问题