Presentation is loading. Please wait.

Presentation is loading. Please wait.

Mutability: C++ const usage SWE 332 Fall 2011 Paul Ammann.

Similar presentations


Presentation on theme: "Mutability: C++ const usage SWE 332 Fall 2011 Paul Ammann."— Presentation transcript:

1 Mutability: C++ const usage SWE 332 Fall 2011 Paul Ammann

2 SWE 3322 Java and C++ Differ in Treatment of Mutability C++ uses the const keyword Much more complex than Java final But, of course, much more flexible Basic rule: Use const wherever possible Rationale: Minimize Mutability Same reasons as given by Bloch for Java Sources Some from Kak Most from Meyer’s Effective C++

3 SWE 3323 C++ const: Minimize Mutability Recalling Bloch’s reasons for Java: Easier to design Easier to implement Easier to use Less prone to error More secure Easier to share Thread safe

4 SWE 3324 Basic usage of const char greeting[] = “Hello”; char *p = greeting; // non-const pointer // non-const data const char *p = greeting; // non-const pointer // const data char * const p = greeting; // const pointer // non-const data const char * const p = greeting; // const pointer // const data

5 SWE 3325 More syntax void f1 (const Widget *pw); // f1 takes a pointer to a // constant Widget object void f2( Widget const *pw); // so does f2 Note: It doesn’t matter if you switch the const and the type. But the location of the * is key.

6 SWE 3326 const_iterator std::vector vec; … // iter acts like a T* const const std::vector ::iterator iter = vec.begin(); *iter = 10; // Ok; changes what iter points to ++iter; // error! iter is const // cIter acts like a const T* std::vector ::const_iterator cIter= vec.begin(); *cIter = 10; // error! *cIter is const ++cIter; //fine, changes cIter

7 SWE 3327 const function returns class Rational {…} const Rational operator*(const Rational& lhs, const Rational&rhs); // why make return const? Rational a, b, c; … (a*b) = c; // oops! //more likely if (a*b=c) … // meant to say (a*b==c) // use of const results in compiler error

8 SWE 3328 const member functions class TextBlock { public: const char&operator[](std::size_t position) const // operator[] for { return text[position];} // const objects // Note the code bloat!– can be solved by “casting away constness” char&operator[](std:size_t position) // operator[] for { return text[position];} // non const objects private: std::string text; … TextBlock tb(“Hello”); std::cout << tb[0]; // calls non-const TextBlock::operator[] const TextBlock ctb(“World”); std::cout<< ctb[0] // calls const TextBlock::operator[] void print(const TextBlock& ctb) // in this function, ctb is constant { std::cout <<ctb[0];…} // calls const TextBlock::operator[] tb[0] = ‘x’; // fine – writing a non const TextBlock ctb[0] = ‘x’; // error! - writing a const TextBlock

9 SWE 3329 Bitwise vs. Logical constness Bitwise constness: member function is const iff it doesn’t modify any of objects data members Doesn’t actually enforce immutability Constant pointers vs. mutable data Logical constness: (preferred) member function may modify bits of the object – as long as client can’t tell keyword mutable used to tell compiler modifications are ok

10 SWE 33210 Prefer pass-by-reference-to- const to pass-by-value C++ default is pass by value: for both parameters and return values can be expensive… bool valStudent(Student s); // pass by value Student plato; bool platoOK = valStudent(plato); // call copy constructor vs. bool valStudent(const Student& s); // no constructor/destructor calls // but still safe

11 SWE 33211 Things to remember Declaring const can help compilers detect usage errors const can be applied to any scope Compilers enforce bitwise constness But you should program with logical constness Prefer pass-by-reference-to-const to pass-by- value more efficient, and avoids other problems (eg slicing)


Download ppt "Mutability: C++ const usage SWE 332 Fall 2011 Paul Ammann."

Similar presentations


Ads by Google