1 Advanced Issues on Advanced Issues on Classes Classes Part 3 Reference variables (Tapestry pp.581, Horton 176 – 178) Const-reference variables (Horton 176 – 178) object sharing: sharing an object/variable among several objects using reference variables and pointers (Tapestry 578 – 583) Static data members in classes (Tapestry 484 – 486, Horton 322 – 325)
Advanced Issues on Classes. Part 3 Reference variables (Tapestry pp.581, Horton 176 – 178) Const-reference variables (Horton 176 – 178) object sharing: sharing an object/variable among several objects using reference variables and pointers (Tapestry 578 – 583) - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
1
Advanced Issues on ClassesAdvanced Issues on Classes
Part 3Reference variables (Tapestry pp.581, Horton 176 – 178)
Const-reference variables (Horton 176 – 178)
object sharing: sharing an object/variable among several objects using reference variables and pointers (Tapestry 578 – 583)
Static data members in classes (Tapestry 484 – 486, Horton 322 – 325)
2
OverviewOverviewIn this part, you are going to see how different instances of a class (p1 and
p2 below) can share the same variable (board, below).
ChessBoard board;
ChessPiece p1, p2;
Normally, if we add a variable, board, to the ChessPiece class, there will be a different board for each ChessPiece. Of course, this is not what we want; we want a single board to shared by all ChessPiece objects.
There are two solutions for this, one is to use what is called reference variables (this is very easy in term of its use, but slightly more tricky in some other aspects) or to use pointers (this is slightly more complicated to use, but very easy to understand and there are no issues to consider).
Reference VariablesReference VariablesIt is possible to refer one variable using another name (i.e. creating an alias)
using reference variables.
Syntax
Type & ref_variable_name = existing_variable;
int x;
int & xr = x;
No new memory allocation is done for xr.
x and xr refer to the same memory locations after the reference variable declaration. Thus when we say x, it means xr and when we say xr, it is also x.
This does not seem very useful now, but will be so when you want to share some data among multiple instances of a class (see shared-unshared toy example). There are some other useful cases as well.
– But before that we will see more on reference variables.
x xr
Reference VariablesReference Variables
The value of reference b cannot be changed after its declaration. – For example, a few lines further, you cannot write:
double c = 2.71;&b = c;
– expecting now b is c. This is a syntax error (let's see this at refvar.cpp)– Everything is said on the declaration line of b: Reference b and variable a are
bound together, not to be ever separated.
What about the following addition to the end of code above? Does it change the values a, b, c?
double c = 2.71;b = c;
– Let's see the answer and another special case on the code (refvar.cpp)
a contains: 89
Example (refvar. cpp)double a = 3.1415927; double &b = a; b = 89; cout << "a contains: " << a << endl; // Displays ?
Reference VariablesReference Variables and Pointers and PointersSo, are reference variables same as pointers?
– Similar concepts but reference variables are NOT pointers.– There are differences.
Pointers need to be dereferenced using the * operator to reach the memory location, but reference variables are already dereferenced.
int * ptr = new int;* ptr = 10;cout << *ptr;
Pointers are more flexible to use. Pointers need not be initialized at declaration (they can be initialized later). Moreover, pointers can be reinitialized to show different locations throughout the program. However, reference variables must be initialized at declaration to an existing variable and cannot refer to another variable later.
Const-Const-Reference VariablesReference VariablesReference variables can be constant, by putting the const keyword before the
variable declaration.
double x = 4.12;const double & y = x;
We already said that binding between reference variable and the existing regular variable cannot change. So, what is constant here?– It is constant such that you cannot change the memory location's value by
using the reference variable name.– But this does not mean that this memory location's value cannot change.– We can change it using other references (e.g. using the actual variable name)
– see refvar.cpp– We cannot assign a const-reference variable to a normal reference variable
double x = 4.12;const double & y = x;y = 3.5; //syntax error, const ref. var. cannot be changedx = 3.5; //legal syntaxdouble & z = y; //syntax error, const-ref. var. cannot be assigned to a regular ref. var.
Reference Reference Parameters: Parameters: reminderreminderReference parameters are used to allow a function to modify a calling variable:
int main () { double k, m; k = 3; m = 4; change (k, m); cout << k << ", " << m << endl; // Displays 30, 4return 0; }
Pointers for sharingPointers for sharing
If you want to share the same object among various instances of a class object, you must use pointers or reference variables– You should prefer reference variables, if you do the binding at the
beginning and do not need to refer to another object during the program.
– Pointers are easier, if you feel comfortable on how to manipulate them.
One example of need of sharing is that of players (class player) sharing the same game board (class board) . We will not follow-up on this example.
In the next example that we will see, kids are intended to share the same toy (e.g. same ball that each of them plays with together), but in the naive version, each of them ends up having a separate ball.
(Un)shared Toy– toy class (from Tapestry)(Un)shared Toy– toy class (from Tapestry)Naive version that does not actually workNaive version that does not actually work
// This program demonstrates the use of reference variables and// pointers for sharing among class objectsclass Toy { public: Toy (const string& name); void Play(); // prints a message void Breaks(); // the toy breaks private: string myName; //name of toy bool isWorking; //working condition};
void Toy::Breaks()// post: toy is broken{ isWorking = false; cout <<endl<< "oops, this " << myName <<" just broke"<<endl<<endl;}
(Un)shared Toy – kid class(Un)shared Toy – kid classclass Kid{ public: Kid (const string & name, Toy & toy); void Play(); private: string myName; //name of the kid Toy myToy; //creates a local copy, this is problematic };
//constructorKid::Kid(const string & name, Toy & toy) : myName(name), myToy(toy){ } //Equivalent to myToy = toy;
void Kid::Play()// post: kid plays and talks about it{ cout << "My name is " << myName << ", "; myToy.Play();}
Does not work as intendedDoes not work as intended
The body of the iterator loop changes as shown on the right.
Thinking Further: How would you use pointers instead of reference variables?
24
Related: Static Data Member of the ClassRelated: Static Data Member of the ClassIn certain cases, you can also use a static data member of the class that keeps its value
from one construction to the next one.– When a data member is declared as static, only one copy of the data is maintained for all
objects of the class.– Static data members are not part of objects of a given class type; they are separate objects.
As a result, the declaration of a static data member is not considered a definition. The data member is declared in class scope, but definition is performed at file scope.
You can also use static data members to count the number of instances of a class that you created by calling the constructor.
The example below assigns a consecutive ID number to the class instances class User{
private: int id; //id of user static int next_id; //common to all instances, but this is not a definition
//this is just a prototype-like declarationpublic: User() //constructs user instances starting from id number 1 { id = next_id; next_id++; //assigns next id to the next instance and then increments } void print () { cout << id << endl; }
};int User::next_id = 1; //this is definition and initialization