1 / 8

const Value

const Value. const declares immutable value void Test(char* t) { t++; // OK, address is not const t[0] = 'a'; // OK, char is not const } void Test2( const char * t) { t++; // OK, address is not const t[0] = 'a'; // NO, char is const }. const Address. typedef char* PCHAR;

dore
Download Presentation

const Value

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. const Value const declares immutable value void Test(char* t) { t++; // OK, address is not const t[0] = 'a'; // OK, char is not const } void Test2(const char* t) { t++; // OK, address is not const t[0] = 'a'; // NO, char is const }

  2. const Address typedef char* PCHAR; void Test3(const PCHAR t) { t++; // NO, address is const t[0] = 'a'; // OK, char is not const } typedef const char* PCONSTCHAR; void Test4(PCONSTCHAR t) { t++; // OK, address is not const t[0] = 'a'; // NO, char is const } void Test5(const PCONSTCHAR t) { t++; // NO, addressis const t[0] = 'a'; // NO, char is const }

  3. & Reference char myChar; char& myChar2 = myChar; // myChar2 points to myChar myChar = ‘a’; // Now myChar2 = ‘a’ myChar2 = ‘b’; // Now myChar = ‘b’ Reference is similar to pointer because it holds address internally Reference is different from pointer because you cannot manipulate (or see) the address Reference is an always-dereferenced pointer Reference cannot be NULL or invalid void Change (char& myChar) { myChar = ‘b’; } void main() { char myChar = ‘a’; Change(myChar);// Now myChar = ‘b’ }

  4. const Reference References provide efficient means for quickly passing data to functions (i.e. the data is passed by address aka by reference). Const make the data you pass read-only to safeguard against accidental modifications. void Change (const char& myChar) { myChar = ‘b’; // No, char is const char tempChar = myChar; // OK, myChar is read-only } void main() { char myChar = ‘a’; Change(myChar);// Now myChar = ‘b’ }

  5. const Reference (contd.) class String { public: String(const String& aString); // Copy constructor void MakeUpper(); // Modifies string to make it upper case int GetLength() const; // Retrieves string length without modifying it }; void Test(String myString) // We pass myString by value hence a copy constructor will be called { // and a new String instance will be constructed on stack myString.MakeUpper(); } void Test2(String& myString) // Copy constructor will be NOT called { // because myString is passed by reference myString.MakeUpper(); } void Test3(const String& myString) // Copy constructor will be NOT called { // because myString is passed by reference myString.MakeUpper(); // NO, MakeUpper() modifies string but we pass it by const reference int length = myString.GetLength(); // OK, GetLength() does not modify String (declared as const) } void main() { String myString(“some text”); Test(myString); // myString = “some text” because we passed it by value Test2(myString); // myString = “SOME TEXT” because we passed it by reference }

  6. Static Members: Shared Among All Class Instances class String { public: int GetLength() const // Instance method { int i; for ( i = 0; Buffer[i] != ‘\0’; i++); // OK, can refer to instance prop. Buffer return i; } static GetLength(const char* buffer) // Static method { int i; for ( i = 0; Buffer[i] != ‘\0’ && I < MaxLength; i++ );// NO, can not refer to instance property return i; // OK, can refer to static MaxLength property } private: const char* Buffer; // Instance property static int MaxLength; // Static property }; void main() { String::MaxLength = 10; // set static property String::GetLength(“some text”); // static method invocation String MyString1, MyString2; // Have the same MaxLength value }

  7. String Class: Quick char* Encapsulation class String { public: String(); // Default constructor String(const char* buffer); // Init constructor public: // Does not encapsulate immutable char pointer const char* Buffer; }; void main() { String myString(“test”); myString.Buffer = “test2”; // We can change Buffer pointer }

  8. String Class: Robust char* Encapsulation class String { public: String(); // Default constructor String(const char* buffer); // Init constructor const char* GetBuffer() const; // Returns encapsulated buffer char operator [](int index) const; // Index operator to retrieve character String& operator = (const char* buffer); // Assignment operator to initialize private: // Encapsulated immutable char pointer const char* Buffer; }; void main() { String myString(“test”); myString.Buffer = “test2”; // NO, Buffer is private const char* p = myString.GetBuffer(); // OK, get encapsulated buffer char c = myString[0]; // Get 0-th character }

More Related