Understanding pointers?

后端 未结 6 395
北恋
北恋 2020-12-11 11:25

As the title suggests, I\'m having trouble understanding exactly what a pointer is and why they\'re used. I\'ve searched around a bit but still don\'t really understand. I\'

6条回答
  •  有刺的猬
    2020-12-11 11:55

    I have heard the analogy that an object is like a ballon, an the string you're holding it with is the pointer. Typically, code is executed like so:

    MyClass *someObj = [[MyClass alloc] init];
    

    The alloc call will allocate the memory for the object, and the init will instantiate it with a defined set of default properties depending on the class. You can override init.

    Pointers allow references to be passed to a single object in memory to multiple objects. If we worked with values without pointers, you wouldn't be able to reference the same object in memory in two different places.

提交回复
热议问题