Presentation is loading. Please wait.

Presentation is loading. Please wait.

A popular USENET joke: In C, you merely shoot yourself in the foot. In C++, you accidentally create a dozen instances of yourself and shoot them all in.

Similar presentations


Presentation on theme: "A popular USENET joke: In C, you merely shoot yourself in the foot. In C++, you accidentally create a dozen instances of yourself and shoot them all in."— Presentation transcript:

1 A popular USENET joke: In C, you merely shoot yourself in the foot. In C++, you accidentally create a dozen instances of yourself and shoot them all in the foot. Providing emergency medical care is impossible, because you can't tell which are bitwise copies and which are just pointing at others and saying, "That's me, over there."

2  Using const is a hassle, but NOT using const can be a bigger problem for you and your client. ◦ Some say: "You can either buy leaky pens and wear a pocket protector, or just buy pens that don't leak, period." ◦ However, when you're writing code, you're not buying pens - you're manufacturing pens for other people to stick in their pockets. Using const helps in manufacturing quality pens that don't leak.

3  Guarantees data integrity ◦ Exercise your right to say "NO! You can't change this value, and NO! I won't change yours." It's only five little letters and a whitespace token.  Makes debugging easier (brings errors closer to the compiler)

4 #include #include "monkey.h" using namespace std; class MonkeyHouse { public: bool containsMonkey( string & name ); //Will it change the value of my name? void addMonkey( Monkey* m ); //Will it change the value of my Monkey? void removeMonkey( Monkey* m ); //Will it change the value of my Monkey? Monkey * getMonkey( string &name ); private: map myMonkeys; }; //if the answer is yes, then when does it change my values -- before or after they //perform is expected function?

5 1. int I = 1; 2. const int I = 2; 3. const int* I = 3; 4. int const * I = 3; 5. int* const I = 2; 6. const int* const I = 1; 7. const int& I = 2; 8. int& const I = 2;

6 const int x; // constant int x = 2; // illegal - can't modify x const int* pX; // changeable pointer to constant int *pX = 3; // illegal - can't use pX to modify an int pX = &someOtherIntVar; // legal - pX can point somewhere else int* const pY; // constant pointer to changeable int *pY = 4; // legal - can use pY to modify an int pY = &someOtherIntVar; // illegal - can't make pY point elsewhere const int* const pZ; // const pointer to const int *pZ = 5; // illegal - can't use pZ to modify an int pZ = &someOtherIntVar; // illegal - can't make pZ point elsewhere

7  A const object must remain const for the remainder of its life.  There is more than one way to make an object constant (example uses class Monkey objects) ◦ declare it const  map myMonkeys; ◦ pass it to a function as const  void Monkey::addMonkey( const Monkey* const m ); ◦ return it from a function as const  const Monkey * GetMonkey( const string &name );

8  All or nothing  Once an object is constant, for the rest of its life, it can: ◦ call const member functions only ◦ be passed to functions that accept const only ◦ be returned from functions as const only

9  To function parameters that will not change data ◦ bool containsMonkey( const string & name );  note: you now have a const reference  To function parameter pointers that will not change pointer location ◦ void addMonkey( const Monkey* const m );  note: you now have a const pointer

10  To functions that are called by const objects (because you have to) ◦ string GetName() const;  because you now have const objects that need it  To return values for const objects (because you have to) ◦ const Monkey * GetMonkey( const string &name );  because your Monkey is now const

11 class Person { public: Person(char* szNewName) ~Person() { delete[] m_szName; }; const char* const GetName() //return value is const, not the function { return m_szName; }; private: char* m_szName; }; //Non Member function implementation void PrintPerson(const Person* const pThePerson)//accepts a const object { // error - non-const member function called cout GetName() << endl; //const object's non-const member function }

12 class Person { public: Person(char* szNewName) ~Person() { delete[] m_szName; }; const char* const GetName() const private: char* m_szName; }; //Non Member function implementation void PrintPerson(const Person* const pThePerson) //takes const object { // no error - const member function called cout GetName() << endl; //const object's const member function }

13  You guarantee (and the compiler will make sure of it) that not a single modification can or will be made for the members of the class in your function.  a const member function will not invoke other member functions that are not const. ◦ Const must be maintained until function returns ◦ If a function can't change an object, it can only call other functions that won't change it either  Const functions will accept and return reference and/or pointers to objects if they are const

14  What is overloading? ◦ More than one version of the same function  Why overload? ◦ A non-const function can return a const or a non- const reference or pointer but... ◦ When const functions return objects (ie. references or pointers to members of the class) they must also be const and... ◦ You have const and non-const objects that need the function

15 int Loan::calcInterest() const //const function returns int { return loan_value * interest_rate; } int& myClass::getData() //non-const function returns non-const & { return data; } const int& myData::getData() const //const function returns const & { return data; }

16  Overload a list_search function that traverses a linked list and returns a reference to a node. A const and non-const version of a list_search function could be used as a subtask of: ◦ display a data item from the list (const) ◦ updating data item in a node (non-const) ◦ insert new node in middle or end of list (non-const)  Examples from the text can be found on p. 236 and 316

17 Cplusplus/2/


Download ppt "A popular USENET joke: In C, you merely shoot yourself in the foot. In C++, you accidentally create a dozen instances of yourself and shoot them all in."

Similar presentations


Ads by Google