OC Objects and anonymous objects

Source: Internet
Author: User

Definitions of OC objects and anonymous object objects and anonymous objects:

When new is an object, the pointer is usually called an object if it is received with a pointer.
If the new object is not received by the pointer, then the object is called an anonymous object.

#import<Foundation/Foundation.h>#import "Iphone.h"#import "Person.h"intMainintargcConst Char*argv[]) {    //1. Accessing objects through pointersiphone *ip = [iphoneNew];//create an object and receive it using the pointer IP    ip->_size = 4.3; This is a special notation for pointer access to struct member variables. OC objects are pointers that store a memory address in the heap. Ip.weight=Ten;//accessing the member variables of an object through point syntax[IP PowerOff];//methods for accessing objects through pointers//2. Anonymous objects, no pointers[IphoneNew];//An object is created here, but the object is not received with a pointer, which is an anonymous objectNote: Each time an anonymous object is created, the object is assigned a different storage space, meaning that each anonymous object is not the same. //3. No two identical anonymous objects[IphoneNew]-&GT;_CPU =5;//assigns a value to the 1th anonymous object _cpu[IphoneNew]->_size =4.0;//assigns a value to the 2nd anonymous object _size[IphoneNew]->_weight = the;//assigns a value to the 3rd anonymous object _weight[[IphoneNew] POWEROFF];//Call the 4th anonymous object Poweroff methodNSLog (@"%@", [IphoneNew]);//Print a 5th anonymous object//guess what, the results are all 0 printed here. //The reason is that each time an assignment is made, a new anonymous object is created, and nslog prints a new anonymous object whose member variables are not assigned a value.       So it's all 0. //4. An anonymous object can be passed as an argumentPerson *p = [personNew]; Iphone*ip = [IphoneNew];    [P SENDSIGNAL:IP]; //here is passed by pointer, as argument[P Sendsignal:[iphoneNew]];//This is done using an anonymous object as an argument pass, but the [Iphone new] object is destroyed after this use, unless a new pointer references it.         return 0;}

OC Objects and anonymous objects

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.