Today’s Topic Const Ref: http://www.learncpp.com/cpp-tutorial/2-9-symbolic- constants-and-the-const-keyword/ http://www.learncpp.com/cpp-tutorial/810-const-class- objects-and-member-functions/ Slides for CS 31 discussion session TA: Hsiao-Yun (Katie) Tseng tsenghy@g.ucla.edu Credit to former TA Bo-Jhang Ho (bojhang@cs.ucla.edu), CS31 Discussion 1E, Spring 17’ Credit to former TA Chelsea Ju (chelsea.ju@cs.ucla.edu)
Constant variables To make a variable constant, simply put the const keyword either before or after the variable type Declaring a variable as const prevents us from inadvertently changing its value: Defining a const variable without initializing it will also cause a compile error: const double gravity = 9.8; // preferred use of const before type int const sidesInSquare = 4 ; // okay, but not preferred const double gravity = 9.8; gravity = 9.9; // not allowed, this will cause a compile error const double gravity; // compiler error, must be initialized upon definition
Pointers to Constant Variables We can change pointer to point to any other variable We cannot change value of object pointed by ptr const int *ptr; // preferred use of const before type int const *ptr; // okay, but not preferred int main() { int x = 100, y = 200; const int * ptr = &x; // ptr is a pointer pointed to a “const int” ptr = &y; // okay. ptr itself is not constant *ptr = 20; // error. ptr is pointed to a “const int”, which is a // constant that cannot be changed return 0; }
Constant Pointers to Variables We cannot change pointer to point to any other variable We can change value of object pointed by ptr int * const ptr = &x; // must be initialized int main() { int x = 100, y = 200; int * const ptr = &x; // ptr is a const pointer pointed to a “int” ptr = &y; // error. ptr itself is a constant pointer *ptr = 20; // ok. ptr is pointed to a “int”, which can be changed return 0; }
Constant Pointers to Constant Variables We cannot change pointer to point to any other variable We can change value of object pointed by ptr const int * const ptr = &x; // must be initialized int main() { int x = 100, y = 200; const int * const ptr = &x; // ptr is a const pointer pointed to a “const int” ptr = &y; // error. ptr itself is a constant pointer *ptr = 20; // error. ptr is pointed to a “const int”, which cannot be changed return 0; }
* const int * ptr; int * const ptr; const int * const ptr; Const and pointers Description of the variable it pointed to * Description of pointert itself const int * ptr; int * const ptr; const int * const ptr;
Constant references Reference must have the same type as the variable it reference to Reference is always a constant; we cannot make a reference refer to a different object We cannot change the object referred by a const reference int main() { const int x = 100; int& ref = &x; // error. Different type const int& ref = &x; // okay ref = 20; // error. Constant value cannot be change return 0; }
Description of the variable it refers to Const and references Description of the variable it refers to & Only reference’s name const int & ref; int & ref;
Constant classes Can this be compiled? #include <iostream> using namespace std; class Date { public: Date(){} Date(int y, int m, int d) : m_year(y), m_month(m), m_date(d) {} void setYear(int y) {m_year = y;} void printMonth() cout << m_month << endl; } void printDate() const cout << m_date << endl; int m_year; private: int m_month; int m_date; }; int main() { const Date date1; date1.m_year = 2011; date1.setYear(2012); const Date date2(2010,10,11); date2.printMonth(); date2.printDate(); return 0; } Can this be compiled?
Constant classes Can this be compiled? #include <iostream> using namespace std; class Date { public: Date(){} Date(int y, int m, int d) : m_year(y), m_month(m), m_date(d) {} void setYear(int y) {m_year = y;} void printMonth() cout << m_month << endl; } void printDate() const cout << m_date << endl; int m_year; private: int m_month; int m_date; }; int main() { const Date date1; date1.m_year = 2011; date1.setYear(2012); const Date date2(2010,10,11); date2.printMonth(); date2.printDate(); return 0; } Can this be compiled?
Constant classes Instantiated class objects can also be made const by using the const keyword Once a const class object has been initialized via constructor, any attempt to modify the member variables of the object is disallowed: const class objects can only call const member functions const Date date1; // initialize using default constructor const Date date2(2010,10,11); // initialize using parameterized constructor date1.m_year = 2011; // compiler error: violates const date1.setYear(2012); // compiler error: violates const date2.printMonth(); // compiler error: violates const date2.printDate(); // compiler ok: printDate is a const member function
Const member functions A const member function is a member function that guarantees it will not modify the object or call any non- const member functions (as they may modify the object) class Date { public: Date(){} void printMonth() {cout << m_month << endl;} void printDate() const {cout << m_date << endl;} void printAll() const m_year = 1111; // const member function cannot modify the object printMonth(); // const member function cannot call non-const member functions printDate(); } private: int m_year; int m_month; int m_date; };