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

Chapter 14: Point and Stare at Objects 187

Student s;

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

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

//(this works as expected)

(*pS).gpa = 3.5;

return 0;

}

The *pS evaluates to the pointer’s Student object pointed at by pS. The

.gpa refers to the gpa member of that object.

Pointing toward arrow pointers

Using the splat operator together with parentheses works just fine for deref­ erencing pointers to objects; however, even the most hardened techies would admit that this mixing of asterisks and parentheses is a bit tortured.

C++ offers a more convenient operator for accessing members of an object to avoid clumsy object pointer expressions. The -> operator is defined as follows:

ps->gpa is equivalent to(*pS).gpa

This leads to the following:

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

{

Student s;

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

// access the gpa member of the object pointed at by pS pS->gpa = 3.5;

return 0;

}

The arrow operator is used almost exclusively because it is easier to read; however, the two forms are completely equivalent.

Passing Objects to Functions

Passing pointers to functions is just one of the ways to entertain yourself with pointer variables.

188 Part III: Introduction to Classes

Calling a function with an object value

As you know, C++ passes arguments to functions by reference when the argu­ ment type is flagged with the squiggly ‘&’ property (see Chapter 8). However, by default, C++ passes arguments to functions by value (you can check Chapter 6, on this one, if you insist).

Complex, user-defined class objects are passed the same as simple int values as shown in the following PassObjVal program:

//PassObjVal - attempts to change the value of an object

//in a function fail when the object is

//passed by value

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

class Student

{

public:

int semesterHours; float gpa;

};

void someFn(Student copyS)

{

copyS.semesterHours = 10; copyS.gpa = 3.0;

cout << “The value of copyS.gpa = “ << copyS.gpa << “\n”;

}

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

{

Student s; s.gpa = 0.0;

//display the value of s.gpa before calling someFn() cout << “The value of s.gpa = “ << s.gpa << “\n”;

//pass the address of the existing object

cout << “Calling someFn(Student)\n”; someFn(s);

cout << “Returned from someFn(Student)\n”;

// the value of s.gpa remains 0

cout << “The value of s.gpa = “ << s.gpa << “\n”;

Chapter 14: Point and Stare at Objects 189

//wait until user is ready before terminating program

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

return 0;

}

The function main() creates an object s and then passes s to the function someFn().

It is not the object s itself that is passed, but a copy of s.

The object copyS in someFn() begins life as an exact copy of the variable s in main(). Any change to copyS made within someFn() has no effect on s back in main(). Executing this program generates the following understandable but disappointing response:

The value of s.gpa = 0

Calling someFn(Student)

The value of copyS.gpa = 3

Returned from someFn(Student)

The value of s.gpa = 0

Press any key to continue . . .

Calling a function with an object pointer

Most of the time, the programmer wants any changes made in the function to be reflected in the calling function as well. For this, the C++ programmer must pass either the address of an object or a reference to the object rather than the object itself. The following PassObjPtr program uses the address approach.

//PassObjPtr - change the contents of an object in

//a function by passing a pointer to the #include <cstdio>

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

class Student

{

public:

int semesterHours; float gpa;

};

190 Part III: Introduction to Classes

void someFn(Student* pS)

{

pS->semesterHours = 10; pS->gpa = 3.0;

cout << “The value of pS->gpa = “ << pS->gpa << “\n”;

}

int main(int nNumberofArgs, char* pszArgs[])

{

Student s; s.gpa = 0.0;

//display the value of s.gpa before calling someFn() cout << “The value of s.gpa = “ << s.gpa << “\n”;

//pass the address of the existing object

cout << “Calling someFn(Student*)\n”; someFn(&s);

cout << “Returned from someFn(Student*)\n”;

// the value of s.gpa is now 3.0

cout << “The value of s.gpa = “ << s.gpa << “\n”;

//wait until user is ready before terminating program

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

return 0;

}

The type of the argument to someFn() is a pointer to a Student object (oth­ erwise known as Student*). This is reflected in the way that the program calls someFn(), passing the address of s rather than the value of s. Giving someFn() the address of s allows him to modify whatever value that is stored there. Conceptually, this is akin to writing down the address of the house s on the piece of paper pS and then passing that paper to someFn(). The function someFn() uses the arrow syntax for dereferencing the pS pointer.

The output from PassObjPtr is much more satisfying (to me anyway):

The value of s.gpa = 0

Calling someFn(Student*)

The value of pS->gpa = 3

Returned from someFn(Student*)

The value of s.gpa = 3

Press any key to continue . . .