ATL-STYLE TEMPLATES
Even if you can read C Templates without getting a headache
About C template definition, ATL has two things that make people distress. Note the following example:
Class CMYWND: PUBLIC CWINDOWIMPL
{
...
}
That actually is legal, because the C spec says that immediately after the class CMyWnd part, the name CMyWnd is defined and can be used in the inheritance list. The reason for having the class name as a template parameter is so ATL can do the second Tricky Thing, Compile-Time Virtual Function Calls.
The above code is legal because C specifies that as long as the Class CMYWND definition section can use CMYWnd, it is of course used in the definition of the base class table. This is why the class name can be used like a template parameter, which is because of such features, ATL has the second piece of design, compile time virtual function call.
(If you can use CMYWND immediately after the Class CMYWND, can you make a class as your own subclass? It seems that you can do it, because the compiler will find the definition of the base class, if you do yourself Base class, the compiler will prompt the base class without defining - snail hand)
To See this in action, Look at this set of classes:
Note the following definition:
Template
Class B1
{
PUBLIC:
Void Sayhi ()
{
T * pt = static_cast
Pt-> PrintClassName ();
}
protected:
Void PrintClassName () {cout << "this is b1";}
}
Class D1: Public B1
{
// No Overridden Functions At All
}
Class D2: Public B1
{
protected:
Void PrintClassName () {cout << "this is d2";}
}
Main ()
{
D1 D1;
D2 D2;
D1.SAYHI (); // Prints "this is b1"
D2.SAYHI (); // Prints "this is d2"
}
The static_cast
you'd be in trouble.) It's safe because the this object can only be of type D1 * or D2 * (as appropriate), and nothing else. Notice that this is almost exactly like normal C polymorphism, except that the SayHi () Method isn't Virtual.
Statement Static_cast