Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C++ For Dummies (2004) [eng].pdf
Скачиваний:
84
Добавлен:
16.08.2013
Размер:
8.09 Mб
Скачать

Chapter 14: Point and Stare at Objects 185

void someFn()

{

//declare an array of 10 students Student s[10];

//assign the 5th student a gpa of 5.0 (lucky guy) s[4].gpa = 5.0;

//add another course to the 5th student;

//this time he failed - serves him right s[4].addCourse(3, 0.0);

}

Here s is an array of Student objects. s[4] refers to the fifth Student object in the array. By extension, s[4].gpa refers to the GPA of the 5th student. Further, s[4].addCourse() adds a course to the 5th Student object.

Declaring Pointers to Objects

Pointers to objects work like pointers to simple types, as you can see in the example program ObjPtr:

// ObjPtr - define and use a pointer to a Student object #include <cstdio>

#include <cstdlib> #include <iostream> using namespace std;

class Student

{

public:

int semesterHours; float gpa;

float addCourse(int hours, float grade){return 0.0;};

};

int main(int argc, char* pArgs[])

{

//create a Student object Student s;

s.gpa = 3.0;

//now create a pointer to a Student object Student* pS;

//make the Student pointer point to our Student object pS = &s;

cout <<

“s.gpa

=

<<

s.gpa

<<

“\n”

<<

“pS->gpa

=

<<

pS->gpa

<<

endl;

186 Part III: Introduction to Classes

//wait until user is ready before terminating program

//to allow the user to see the program results system(“PAUSE”);

return 0;

}

The program declares a variable s of type Student. It then goes on to declare a pointer variable pS of type pointer to a Student object, also written as Student*. The program initializes the value of one of the data members in s. It then precedes to assign the address of s to the variable pS. Finally, it refers to the same Student object, first using the object’s name, s, and then using the pointer to the object, pS. I explain the strange notation pS->gpa; in the next section of this chapter.

Dereferencing an object pointer

By analogy of pointers to simple variables, you might think that the following refers to the GPA of student s:

int main(int argc, char* pArgs[])

{

//the following is incorrect Student s;

Student* pS = &s; // create a pointer to s

//access the gpa member of the object pointed at by pS

//(this doesn’t work)

*pS.gpa = 3.5;

return 0;

}

As the comments indicate, this doesn’t work. The problem is that the dot operator (.) is evaluated before the pointer (*).

Note: The * operator is often referred to as the splat operator — not a popu­ lar term with insects.

C++ programmers use parentheses to override the order in which operations are performed. For example, the parentheses force addition to be performed before multiplication in the following expression:

int i = 2 * (1 + 3); // addition performed

// before multiplication

Parentheses have the same effect when applied to pointer variables:

int main(int argc, char* pArgs[])

{