1 / 17

const Security

A popular USENET joke: In C, you merely shoot yourself in the foot.

Download Presentation

const Security

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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." const Security

  2. Why Use const? • 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. Why Use const? • 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 <map> #include <string> #include "monkey.h" using namespace std; class MonkeyHouse { public: boolcontainsMonkey( 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< string, Monkey* > myMonkeys; }; //if the answer is yes, then when does it change my values -- before or after they //perform is expected function? Example

  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; Const Everywhere

  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 Const Enforcement

  7. Constant Objects • 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< string, const Monkey* > 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. Constant Objects • 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. Cascading Constants: Where to add const? • To function parameters that will not change data • boolcontainsMonkey( 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. Cascading constants: Where to add const? • 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 http://www.timfanelli.com/main/Notebook/Entries/2005/10/17_The_C++_const_Keyword.html

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

  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 << pThePerson->GetName() << endl; //const object's const member function } Example

  13. Constant Member Functions • 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. Overloading Functions for Const • 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; } Examples

  16. Example of When To Overload for const • 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. http://www.devarticles.com/c/a/Cplusplus/Const-Correctness-in-Cplusplus/2/http://www.devarticles.com/c/a/Cplusplus/Const-Correctness-in-Cplusplus/2/ http://www.possibility.com/Cpp/const.html http://www.timfanelli.com/item/110 References

More Related