In OC, it is easy to use pointers to modify object attributes. Check whether you have used a gun or not.

Source: Internet
Author: User

In OC, it is easy to use pointers to modify object attributes. Check whether you have used a gun or not.

For beginners, it is generally not a problem to master how to modify object attributes in the OC pointer. But how many "functions" and "tossing" do you need? Haha! Let's see if you have never been shot again! See the following code:

void test1(int newAge,double newHeight);
void test2(Person *newP);
void test3(Person *newP);
void test4(Person *newP);
int main(){
Person *p= [Person new];
    p->_age = 10;
    p->_height = 1.2f;
test1(p->_age, p->_height);
    [p print];
     test2(p);
    [p print];
    test3(p);
     [p print];
    test4(p);
     [p print];
    return 0;
}
void test1(int newAge,double newHeight)
{
    newAge = 30;
    newHeight = 1.4;
}
void test2(Person *newP)
{
    newP->_age = 20;
    newP->_height = 3.3;
}
void test3(Person *newP)
{
    Person *p2 = [Person new];
    p2->_height = 4.4;
    p2->_age = 50;
    newP = p2;
    newP ->_age = 60;
}
void test4(Person *newP)
{
    Person *p2 = newP;
    p2->_age = 18;
    p2->_height = 5.1;
    newP->_age =25;
}

Confident friends can write down your answers in silence. Let's answer your questions.

Age = 10, height = 1.200000

Age = 20, width = 3.300000

Age = 20, width = 3.300000

Age = 25, height = 5.100000

Principle:Before taking a look at the principle, we must first understand one thing: After an object is created, the system will do three things for us: 1. it will open up a storage space in the heap (the heap has a feature, that is, it will not be automatically released) 2. will initialize the object property to zero 3. an address is returned to the object (if each attribute shows an element, The 0th elements are the system-generated address "isa", which is the object address)

 

Test1: Open up a bucket in the stack and assign values of 10 and 1.2f to newAge and newHeight. After the function call ends, the test1 function is released.

Test2: Assign p to Person * newP, so * newP is equal to p, which is equivalent to accessing the bucket pointed to by p through newP. After the function call ends, the test2 function is released.

Test3: (I think this is complicated, and it is easy to get into the misunderstanding.) Assign the value of p to newP in test3. newP points to [Person new], in test3, a new pointer p2 points to the newly opened [Person new] In the heap, and then assigns the p2 value to newP (even if it overwrites the original address ), therefore, newP points to [Person new], so the age accessed through newP is the age in [Person new, the attribute in [Person new] is not affected, but test2 has been called above, so the property result is the same as above.

Test4: It is equivalent to directly assigning the value of newP to p2. It is one thing to modify object attributes through p2 or newP.

If you have questions, it may be easier to draw a picture by yourself. You can also discuss with me and make progress together!

Now, we are here today. If there is anything wrong with it, I hope you can look at the official site. Thank you!

---- Nevenmoore


Related Article

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.