O'Reilly logo

Essential C++ by Stanley B. Lippman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

6.6. Constant Expressions and Default Parameters

Template parameters are not limited only to types, although until now I’ve limited my discussion to them. We can also declare constant expressions as parameters. For example, our numeric sequence class hierarchy might be redefined as a template class in which the number of elements an object contains is parameterized:

template <int len> 
class num_sequence { 
public: 
    num_sequence( int beg_pos=1 ); 
    // ... 
}; 

template <int len> 
class Fibonacci : public NumericSeries<len> { 
public: 
    Fibonacci( int beg_pos=1 ) 
         : num_sequence<len>( beg_pos ){} 
    // ... 
}; 

When a Fibonacci object is created, as in

Fibonacci< 16 > fib1; 
Fibonacci< 16 > fib2( 17 ); 

instances of both the Fibonacci derived class and the ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required