//be a polymorphic type.
//additional class details omitted
};
class Derived : public Base {
// class details omitted
};
void Base::f()
{
// Define function from Base.
}
int main ()
{
Base *p;
//code which does either
//p = new Base; or
//p = new Derived;
if (dynamic_cast <Derived *> (p))
cout << “Derived (or class derived from Derived) Object\n”; else
cout << “Base Object\n”;
}
volatile Keyword
The volatile keyword is used in declarations. It tells the compiler not to do aggressive optimization because a value might be changed in ways the compiler cannot detect.
This keyword is part of the ANSI C standard with the same syntax and semantics.
Usage
Objects that are hardware addresses or those used by concurrently executing pieces of code are frequently declared volatile. Examples are an address used for the current clock time, objects used by a signal handler, or objects used for memory mapped I/O.
NOTE: You can declare an identifier to be both const and volatile. This declares a value that the program cannot change but which can be changed by some means external to the program (such as by a piece of hardware like a clock).
Example
class C {
public:// public to make example simpler volatile int i;
//other class details omitted
};
C someData[10];
int main ()
{
int j = someData[5].i;
j = someData[5].i; | // Without the volatile specifier, |
| // the compiler could optimize these |
| // two statements into one. With it, |
| // it must execute both in case the |
| // i field of someData[5] has changed |
| // by some other means. |
} |
|
148 Standardizing Your Code