lecture28 Static and Const

lecture28 Static and Const - CMPT 128: Introduction to...

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

View Full Document Right Arrow Icon
1 © Janice Regan, CMPT 128, February. 2007 CMPT 128: Introduction to Computing Science for Engineering Students Static, Constant
Background image of page 1

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

View Full DocumentRight Arrow Icon
© Janice Regan, CMPT 128, February 2007 2 Call by reference: objects Why is it more efficient for your function to use a call by reference for an object? The object has several attributes, expressed as class data members. Copying a reference to this block of data is more efficient than copying the whole block of data If you pass by reference you can change the values of the member variables This is an advantage only if you need to change the values If you do not need to change the values then this just provides an addition opportunity for errors to be made
Background image of page 2
© Janice Regan, CMPT 128, February 2007 3 The const Parameter Modifier Large data types (typically classes) Desirable to use pass-by-reference Even if function will not make modifications Protect argument Use constant parameter Also called constant call-by-reference parameter Place keyword const before type Makes parameter "read-only" Attempts to modify result in compiler error
Background image of page 3

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

View Full DocumentRight Arrow Icon
© Janice Regan, CMPT 128, February 2007 4 Using const We have previously discussed the use of const to modify a parameter in a function definition or declaration: To review We declare an object in our calling function, say Rectangle window; We can use the object window as an argument of a function myResult = Funct1( window);
Background image of page 4
© Janice Regan, CMPT 128, February 2007 5 Using const (1) myResult = Funct1( window); int Funct1( Rectangle& window); if this is the definition of Funct1 then we can modify the member variable values like length inside Funct1 int Funct1( const Rectangle &window); if this is the definition of Funct1 then we can not modify the member variable values inside Funct1 Passing by value forces a copy of the whole object to be made and is thus less efficient. The more efficient passing by reference is used here.
Background image of page 5

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

View Full DocumentRight Arrow Icon
© Janice Regan, CMPT 128, February 2007 6 Using const (2) const can also be used in other contexts class Rectangle { `public: int getLength() const; void print() const; //not a complete list of methods
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 05/18/2010 for the course CMPT 128 taught by Professor Regan during the Spring '08 term at Simon Fraser.

Page1 / 20

lecture28 Static and Const - CMPT 128: Introduction to...

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

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