Course Hero Logo

151 pointers to objects and object arrays 394 acctptr

This preview shows page 405 - 407 out of 634 pages.

15.1. POINTERS TO OBJECTS AND OBJECT ARRAYS394acct_ptr =newAccount("Moe", 400, 1300.00);Note that we include the arguments expected by theAccountclass constructor. This statement allocatesmemory for oneAccountobject and executes the constructor’s code to initialize the newly created ob-ject. Figure 15.1 illustrates a pointer to an account object. As with any dynamically allocated entity, a'J''o''e''\n'0123nameacct_ptr400idbalance1300.00Figure 15.1: A pointer to anAccountobjectprogrammer must be careful to use thedeleteoperator to deallocate any objects created vianew.C++supports vectors and arrays of objects, but they present special challenges. First, consider a simplesituation. ThePointclass defines no constructor, so the following code is valid:vector<Point> pts(100);//OkayThe compiler happily generates code that at run time will allocate enough space for 100Pointobjects.No special initializations are needed sincePointhas no constructor. What if a class defines a constructorthat accepts arguments and does not supply also a constructor that requires no arguments? Consider theAccountclass. The following statement is illegal:vector<Account> accts(100);//Illegal, the Account class has//no default constructorWhen creating the space for theacctselements, C++expects a default constructor properly to properlyinitialize all of the vector’s elements. The only constructor in theAccountclass requires arguments, andso the compiler refuses to accept such a declaration. The compiler cannot ensure that the vector’s elementsare properly initialized before the programmer begins using the vector.One solution uses an vector of pointers, as invector<Account*> accts(100);//A vector of account pointersNote that this does not create anyAccountobjects. The programmer subsequently must iterate throughthe vector and usenewto create individually each account element. An example of this would bevector<Account*> accts(100);//A vector of account pointersfor(inti = 0; i < 100; i++) {//Get information from the usercin >> name >> id >> amount;©2014 Richard L. HaltermanDraft date: January 26, 2014
15.1. POINTERS TO OBJECTS AND OBJECT ARRAYS395//Create the new account objectaccts[i] =newAccount(name, id, amount);}The dot operator syntax to access a field of a object through a pointer is a bit awkward:Point*p =newPoint;(*p).x = 253.7;(*p).y = -00.5;The parentheses are required since the dot (.) has higher precedence than the pointer dereferencing operator(*). Without the parentheses, the statement would be evaluated as if the parentheses were placed as shownhere*(p.x) = 253.7;//ErrorThe variablepis not aPointobject, but a pointer to aPointobject, so it must be dereferenced withthe*operator before applying the.operator. C++provides a simpler syntax to access fields of an objectthrough a pointer. Thestructure pointer operatoreliminates the need for the parentheses:Point*p =newPoint;p->x = 253.7;p->y = -00.5;The pair of symbols->constitute one operator (no space in between is allowed), and the operator is meantto look like an arrow pointing right. There is no associated left pointing arrow in C++.

Upload your study docs or become a

Course Hero member to access this document

Upload your study docs or become a

Course Hero member to access this document

End of preview. Want to read all 634 pages?

Upload your study docs or become a

Course Hero member to access this document

Term
Fall
Professor
Chief keef
Tags
Computer program, Richard L Halterman

Newly uploaded documents

Show More

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture