Pointers to Classes


As you have previously seen, you can make a pointer to any data type. And as we have already discussed, a class is simply a data type, it’s just a lot more complicated than most. This means you can use pointers to classes. You have already seen an example shown with the transfermoney function. Recall that the purpose of a class is to take the data and functions that work on the data and encapsulate them together. If you require a function to have access to both data and functions, it is only logical to pass it a pointer to a class.

The pointer to a class operates just like the pointer to a structure. You use the -> operator rather than the . operator to access the various members of the class pointer. The following brief code segment illustrates this.

void funca(myclass *myobj) { myobj->element1; myobj->element2; }

As you can see, a pointer to a class works much like a pointer to a structure. The use of class pointers can be powerful because it allows you to pass an entire class, with all its data and methods, into a function. And because you are using a pointer, you don’t need to worry about returning all those values.




C++ Programming Fundamentals
C++ Programming Fundamentals (Cyberrookies)
ISBN: 1584502371
EAN: 2147483647
Year: 2005
Pages: 197
Authors: Chuck Easttom

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net