ATL STYLE Template Learning

xiaoxiao2021-03-06  78

ATL STYLE Template Learning

ATL's template definition is dazzling, such as,

Class CMYWND: PUBLIC CWINDOWIMPL

{

...

}

It is said that this is legal because the C grammatical explanation, even if the CMYWnd class is only partially defined, class name CMYWND has been included in the recursive inheritance list, which is available.

The parameters of the class name as a template class are because ATL is called in the compilation period. The real virtual function, its polymorphism is determined at runtime.

To this end, the program has been viewed:

// atLTemplate.cpp: Defines the entry point of the console application.

//

#include "stdafx.h"

Using std :: cout;

Using std :: end1;

Class Base

{

PUBLIC:

Void syhi () {printclassname ();

protected:

Void PrintClassName () {cout << _t ("My class name: / tbase") <

}

Class D1: Public Base

{

PUBLIC:

protected:

Void PrintClassName () {cout << _t ("My Class Name: / TD1") <

Private:

}

Class D2: Public Base

{

PUBLIC:

protected:

Private:

}

Template

Class Baset

{

PUBLIC:

Void Sayhi ()

{

T * pt = static_cast

(this);

Pt-> PrintClassName ();

}

protected:

Void PrintClassName () {cout << _t ("My class name: / tbaset") <

}

Class TD1: Public Baset

{

PUBLIC:

// protected:

Void PrintClassName () {cout << _t ("My Class Name: / TTD1") <

Private:

}

Class TD2: Public Baset

{};

Class TD3: Public Baset

{

}

INT _Tmain (int Argc, _tchar * argv [])

{

D1 D1;

D2 D2;

D1.SAYHI ();

D2.SAYHI ();

// ----------

TD1 TD1;

TD2 TD2;

TD1.SAYHI ();

Td2.SAYHI ();

// ----

TD3 TD3;

TD3.SAYHI ();

GetChar ();

Return 0;

}

The final output:

MY Class Name: Base

MY Class Name: Base

My class name: TD1

My class name: baset

My class name: TD1

For ordinary class Bases, call the printclassName () function in the base class base :: Sayhi (), you can only call your own function: Base :: PrintClassName

So D1 has defined PrintClassName, but is not called.

If you add the PrintClassName in Base, the result is not the same. Result D1 call is a function of its own class D1 :: PrintClassName;

转载请注明原文地址:https://www.9cbs.com/read-93176.html

New Post(0)