type, however, and the restricted access and strong typing makes static data safer
than global data.
Q Why use static member functions when I can use global functions?
A Static member functions are scoped to the class and can be called only by using an
object of the class or an explicit full specification (such as
ClassName::FunctionName()).
Q Is it common to use many pointers to functions and pointers to member
functions?
A No, these have their special uses, but are not common constructs. Many complex
and powerful programs have neither. There might, however, be times when these
offer the only solution.
Workshop ............................................................................................................
The Workshop contains quiz questions to help solidify your understanding of the mater-
ial covered and exercises to provide you with experience in using what you’ve learned.
Try to answer the quiz and exercise questions before checking the answers in Appendix
D, and be certain you understand the answers before going to tomorrow’s lesson.
Quiz ................................................................................................................
- Can static member variables be private?
- Show the declaration for a static member variable called itsStaticthat is of
type int. - Show the declaration for a static function called SomeFunctionthat returns an inte-
ger and takes no parameters. - Show the declaration for a pointer to function returning longand taking an integer
parameter. - Modify the pointer in Question 4 so it’s a pointer to member function of class Car.
- Show the declaration for an array called theArraythat contains 10 pointers as
defined in Question 5.
Exercises ........................................................................................................
- Write a short program declaring a class with one member variable and one static
member variable. Have the constructor initialize the member variable and incre-
ment the static member variable. Have the destructor decrement the member
variable.
534 Day 15