Use of person in BMITime


#1

I have a few questions related to the use of person in BMITimw.

  1. After we have created the Employee class I was wondering why we still us person instead of employee when creating instances of Employee.

For example why not use Employee *employee = [[Employee alloc] init];? This seemed more consistent with earlier naming conventions.

  1. When creating an instance of Employee in the loop

for (int i = 0; i < 10; i++) {

        // Create an instance of Employee
        Employee *person = [[Employee alloc] init];
        
        // Give the instance variables values
        [person setWeightInKilos:90 + i];
        [person setHeightInMeters:1.8 - i/10.0];
        
        [person setEmployeeID:i];
        
        // Put the employee in the employees array
        [employees addObject: person];
        
    }

the same pointer variable person is used to create distinct instances. I had the impression that there was a connection between the variable pointer and the instance of the Employee object it points to. For example I had expected something like this in the body of the loop:

Employee *person(i) = [[Employee alloc] init];

That is I expected each instance of Employee to have a particular pointer associated with it. If I were to dereference the variable person which instance does it point to? I assumed that one could keep track of individual instances of Employee via individual pointers that point to them but this does not seem to be the case. Rather it seems that when creating multiple instances of an object one has to make sure there is some type of instance variable within the objects like employeeID to keep track. I realize that in this case one could also use the position it the array but that may not always be the case.

It seems that Employee *person = [[Employee alloc] init]; behaves very much like a function in math where " i " is like a suppressed independent variable and “person” is the dependent variable.

So I am trying to make sense of this and understand what does person point to? From the construction I assume the last instance of Employee in the loop. But then, other than the array indexes what points to the others if anything at all?

BTW I really like the book. I had started with another one which did not cover C first and I am really glad I switched. I feel like I get a much better understanding from this one.

Edit:
Given more thought to this I was thinking from the standpoint of memory it does not make sense to have some permanent pointer like person(i) pointing to each instance. Once the instance is in the array the index can serve this purpose. If I am on the right track does the addObject method have a way of “clearing” the person pointer once the instance is added to the array? I was thinking the other possibility would be that with each iteration of the loop as I suggested above, but that would leave the last instance with the pointer person still pointing to it which does not make sense from a memory standpoint.


#2

[quote=“tmadden”]I have a few questions related to the use of person in BMITimw.

  1. After we have created the Employee class I was wondering why we still us person instead of employee when creating instances of Employee.

For example why not use Employee *employee = [[Employee alloc] init];? This seemed more consistent with earlier naming conventions.
[/quote]

I can’t speak to Aaron’s choices directly, but it’s useful to provoke thought by leaving it as person. The fact that an Employee object can be named person has led to questions on the forum that indicate that new programmers have a hard time distinguishing a variable from the object to which it’s pointing.

[quote]
… snip …
Given more thought to this I was thinking from the standpoint of memory it does not make sense to have some permanent pointer like person(i) pointing to each instance. Once the instance is in the array the index can serve this purpose. If I am on the right track does the addObject method have a way of “clearing” the person pointer once the instance is added to the array? I was thinking the other possibility would be that with each iteration of the loop as I suggested above, but that would leave the last instance with the pointer person still pointing to it which does not make sense from a memory standpoint.[/quote]

I believe you’re assessing it correctly. Each array member is a distinct pointer to a different Employee object.

The fact that there are two different places in memory referencing the last object in the array (the array itself and the person variable, as you indicate) is only true until the completion of the loop. The person variable is declared within the loop, so as soon as the last iteration is complete, the variable vanishes into the aether.

It’s not unusual to have multiple variables referencing the same object; it’s purely a question of whether the developer(s) managing the code can get confused, the compiler and machine running the code don’t care.

There’s probably a way to reference the array pointer directly (no reference handy) so that you could skip the person variable, but that would almost certainly be much wordier and more confusing.


#3

Thanks so much. Now I see it. The person variable is effectively a local variable for the loop.

It seems the the idea is to make an object of a particular class as general as possible and then have instance variables within the object, or pointers to other objects, that distinguish individual instances by the settings of these variables. The characteristics of the instance so to speak. This is perfectly natural I suppose because that is how they occur in the real, but for some reason I find it surprising that it can be done in programming. It’s remarkable!