L16_advanced

L16_advanced - 1 Advanced C++ For : COP 3330. Object...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 Advanced C++ For : COP 3330. Object oriented Programming (Using C++) http://www.compgeom.com/~piyush/teach/3330 Piyush Kumar Source: Lutz Kettner. The user. #define private public #define protected public #define class struct Types of users | We can distinguish between to kinds of protection a design can provide: z an user that makes occasionally mistakes z an user that willingly tries to get around the protection mechanism Const correctness // the pointer, the data it refers to // --------------------------------------- int* p; int* const q; const int* r; const int* const s; Revision. // the pointer, the data it refers to // --------------------------------------- int* p; // non-const non-const int* const q; // const non-const const int* r; // non-const const const int* const s; // const const Const declaration | struct A { const int i; A() : i(42) {} }; This pointer and const. | struct C { // hidden parameter: T* const this; void foo(); // hidden parameter: const T* const this; void bar() const; }; 2 Make Temporary Return Objects in C++ Classes Const | L-values: can be used for the left side of an assignment, they are non-const. | R-values: cannot be used for the left side of an assignment. . They are const. | For example the post-increment operator requires an l-value, but is itself an r-value. Make Temporary Return Objects in C++ Classes Const | Thus, we cannot write: z int i; i++ ++; // second ++ forbidden! z Or i++++; z Error: error.cpp:5: error: non-lvalue in increment | But: z struct A { A operator++ (int); // the post increment operator }; z Now, lets try: A a; a++++; //compiles! Make Temporary Return Objects in C++ Classes Const | It works, because a++ returns a temporary object of type A. | But it probably does not do what one would expect. | Since the second ++ works on a temporary object, a itself gets only incremented once. | We can forbid the second increment explicitly by making the return type, the type of the temporary object, const. This should be considered for all similar temporary return types. struct A { // the post increment operator const A operator++ (int); }; Empty Classes #include <iostream> using namespace std; struct X { }; int main(){ cout << sizeof(X) << endl; return 0; } C++ classes are often “empty”! Empty Classes #include <iostream> using namespace std; struct X { }; class Y:public X { }; int main(){ cout << sizeof(X) << endl; cout << sizeof(Y) << endl; return 0; } EBCO: Empty base class Optimization Polymorphism | Recap: Ability to associate different specific behaviors with a single generic notation....
View Full Document

This note was uploaded on 02/27/2012 for the course CS 251 taught by Professor Staff during the Fall '08 term at Purdue.

Page1 / 7

L16_advanced - 1 Advanced C++ For : COP 3330. Object...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online