CSE687 - Object Oriented Design

Language Note #1
Distinguish between value types and reference types

Revised: 4/28/2011
Home Courses Code Handouts CoreTech Books Articles Math Research Masters Projects SWDev WebDev News OtherLinks SiteDesign Graduates AllPages Office Hours Notices

CSE681-SWMAA CSE686-IP CSE687-OOD CSE775-DO CSE776-DP CSE778-AWP CSE784-SWS

Cncp #01 Cncp #02 Cncp #03 Cncp #04 Cncp #05 Cncp #06 Cncp #07 Cncp #08 Cncp #09 Cncp #10 Cncp #11
Note #01 Note #02 Note #03 Note #04 Note #05 Note #06 Note #07 Note #08
Lang #01 Lang #02 Lang #03 Lang #04

Syllabus SG - Design SG - Templates SG - Class Relationships

Language Note #1:

Value types act like a language's primitive types - ints, doubles, etc - supporting copy and assignment operations that operate on their values. Reference types act like pointers. Their state is stored in memory and accessed through pointers stored elsewhere. Assignment and copy operations simply assign and copy pointers, so both source and destination refer, after the operation, to the same object by pointing to its memory location. See the Figure on the left, below, for an example.
image file not found image file not found
Shallow and Deep Copies

Conclusions:

Make a concious choice between value or reference behavior for your classes.
  1. To implement value semantics you endow the class with correct copy, assignment, and destruction operations that keep the class's state distinct for each of its instances. If all the objects held by your class, as data members, have correct copy, assignment, and destruction semantics, then the default operations provided by the compiler will be correct and you don't declare those operations.
  2. You implement reference semantics by sharing state among instances via pointers and managing their destruction with reference counting. Copy, assignment, and destruction must be explicitly defined by your class to support this sharing.
  3. In the rare cases when you do not wish to enforce either value or reference semantics you declare a copy constructor and assignment operator as private. This causes compile failure if a user tries to assign or construct from a copy.

    You should also avoid providing any implementation so that if your class code does an implicit copy or assignment the package will suffer a linking failure during build.