When to use Pointer-to-Pointer in C++?

后端 未结 6 1639
小鲜肉
小鲜肉 2021-02-13 20:38

I was wondering when we use Pointer to Pointer in C++ and why we need to point to a pointer? I know that when we point to a pointer it means we are saving the memory address of

6条回答
  •  难免孤独
    2021-02-13 20:48

    Say you wanna instantiate an object in C++...

    MyClass * obj = new MyClass();
    

    You have to do this because new returns a pointer to the allocated object in dynamic memory. The following would be wrong:

    MyClass obj = new MyClass(); // wrong. 'new' returns a pointer.
    

    Say you want an array of objects...

    MyClass ** objArray = new MyClass*[10];
    

提交回复
热议问题