Docsity

Pointer Refresher - Algorithms and Abstract Data Structures - Lecture Slides, Slides of Data Structures and Algorithms

Partial preview of the text

Download Pointer Refresher - Algorithms and Abstract Data Structures - Lecture Slides and more Slides Data Structures and Algorithms in PDF only on Docsity!

CPSC 223

Algorithms & Data Abstract Structures

Lecture 4: 

Operator overloading (cont.)

and pointer refresher

Today …

• Homework

  • Homework 1 due today
  • Homework 2 assigned
    • Also: finish operators for Key and Entry

• Continue with operator overloading

• Pointer refresher

• Readings

  • Ch. 4: 4.1 - 4. CPSC 223, Fall 2010 2

Stock example again …

Lets add a “<” operator to our Stock class …

class Stock { public: int getShares() const; double getShareValue() const; ... }; // in Stock.cpp: bool operator<(const Stock& s1, const Stock& s2) { ... what should go here??? } CPSC 223, Fall 2010 3

Stock example again …

Lets add a “<” operator to our Stock class … class Stock { public: int getShares() const; double getShareValue() const; ... }; // in Stock.cpp: bool operator<(const Stock& s1, const Stock& s2) { double v1 = s1.getShares() * s1.getShareValue(); double v2 = s2.getShares() * s2.getShareValues(); return v1 < v2; } CPSC 223, Fall 2010 4 Note similar to “worthMore” function

The (implicit) this pointer

  • Every function has an implicit argument named this class Stock { public: int getShares( const Stock* this ) const; double getShareValue( const Stock* this ) const; ... };
  • The this variable is a pointer to an object of the class
    • In this case, a pointer to a Stock object
  • Because it is a pointer, we call it the “ this pointer ” CPSC 223, Fall 2010 7

The (implicit) this pointer

  • Every function has an implicit argument named this class Stock { public: int getShares( const Stock* this ) const; double getShareValue( const Stock* this ) const; ... };
  • Conceptually how the this pointer works: Stock s1; int x = s1.getShares(); int x = getShares(s1); CPSC 223, Fall 2010 8 s1 is the “ invoking object ” The method is invoked on s “rewritten”

Using the this pointer

We can use the this pointer within member functions

  • E.g., to return the “current” (invoking) object const Stock& Stock::worthMore(const Stock& s) const { double v1 = getShares() * getShareValue(); double v2 = s.getShares() * s.getShareValue(); if(v1 < v2) return s; else return ???; // want to return current object } CPSC 223, Fall 2010 9

Using the this pointer

We can use the this pointer within member functions

  • E.g., to return the “current” (invoking) object const Stock& Stock::worthMore(const Stock& s) const { double v1 = getShares() * getShareValue(); double v2 = s.getShares() * s.getShareValue(); if(v1 < v2) return s; else return *this; // want to return current object } CPSC 223, Fall 2010 10 De-references the pointer, giving the current object!

Overloading the Assignment operator

  • What does the implicit assignment operator (=) do?
    • Generally assigns a value to a storage location Stock s1, s2; ... s1 = s2; // s1 now contains a “copy” of s
    • For objects, performs a member-to-member copy (using =)
  • Problems occur for objects with pointers as members
    • After assignment, both objects will point to the same values CPSC 223, Fall 2010 13 int val1 = 10 string* val2; Object int val1 = 10 string* val2; Object “foobar” After object2 assigned to object1

Overloading the Assignment operator

Overloading (=) gives us control of the copy process

  • The basic signature: ClassName& operator=(const ClassName& rhs)
    • E.g.: Stock& operator=(const Stock& rhs)
  • The general pattern: ClassName& ClassName::operator=(const ClassName& rhs) { if(this == &rhs) return *this; // rhs is invoking object ... do the copy ... return *this; // return current object } CPSC 223, Fall 2010 14

Overloading the Assignment operator

When do we override assignment?

  • When class members are pointer types
    • E.g., linked lists, dynamic arrays, and other dynamic structures
  • However, standard to always override
    • Instead of relying on implicit copy CPSC 223, Fall 2010 15

Copy Constructor

The copy constructor is very similar to assignment

Stock s2(s1);

  • Here, we copy s1’s values when constructing s
  • Copy constructor signature: ClassName(const ClassName& c); E.g., Stock(const Stock& s);
  • The main difference is that we do not need to :
  • Check if s is the current object
  • Return the current object CPSC 223, Fall 2010 16

Pointer Refresher

  • For ordinary variables, the C++ compiler allocates

memory to hold the value

int x;

  • Assigning a value to the variable places the value in

the allocated memory space

x = 5; CPSC 223, Fall 2010 19 memory locaNon @111 to hold an int 5 value placed in the memory locaNon @

Pointer Refresher

  • A pointer variable contains the location (or address )

of a memory location

int x; int* xptr;

  • Assigning a value to the variable places the value in

the allocated memory space

x = 5;  xptr = &x; CPSC 223, Fall 2010 20 memory locaNon @ to hold an int 5 value placed in the memory locaNon @ memory locaNon @ to hold an int address @ x’s address placed in the memory locaNon @

Pointer Refresher

  • We can dereference a pointer variable to access the

value stored in the memory location it points to

int x = 5; int* xptr = &x; *xptr = 6; CPSC 223, Fall 2010 21 5 value placed in the memory locaNon @ @ x’s address placed in the memory locaNon @ 6

Pointer Refresher

  • We can also dynamically allocate memory at runtime

using the new keyword, which returns a pointer

variable

int* xptr = new int; *xptr = 5; CPSC 223, Fall 2010 22 value placed in the memory locaNon @ @ x’s address placed in the memory locaNon @ 5