const - // compiler error / sub1( myConst ); s // a...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
// const.cpp / // Shows how const is used with primitives // (called "intrinsic types" in C++). / #include <iostream> using namespace std; u void sub1( const int n ) { //n = 99; // compiler error / cout << "inside sub1: " << n << endl; } int sub2( int n ) { n = 99; cout << "inside sub2: " << n << endl; return n; } void const_examples( ) { const int myConst = 20; //myConst = 30;
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: // compiler error / sub1( myConst ); s // a non-const value can be passed to a function // that expects a constant int myVar = 30; i sub1( myVar ); s // a const value can be passed to a function // that expects a non-constant sub2( 30 ); s // but it cannot accept a non-const return value // myConst = sub2( 22 ); / }...
View Full Document

Ask a homework question - tutors are online