Home

OFF-SOFT.net

OFF-SOFT.net

This site is support & information site of WEB,and Software. This site might help you that create software or Web Site…perhaps?[:]

From WTL to MFC (1) ATL basic

Published on| April 29th, 2009 | No Comment.
Description:
WTL Environment, wrote in the articles below. So, WTL and I really want to treat.
Before that, WTL on mechanisms, ATL is a need for some level of understanding. Therefore a good article (See article below) that there are, help them a little, ATL and deepen their understanding.
Subject of this article, MFC with WTL programmers are those who are interested.
If you are fluent in English, you might be able to sense if you read the referenced article. (This article contents is almost similar to those in English. )

Features of ATL
MFC's programmer, ATL features of novelty,
  • Virtual functions are used more safely using a template
The point is, in the first article had to say. I think so.

Now the difference is, I see differences in the source code.
(This is the source code, public domain are diverted to see what the article is provided)

1.MFC way of inheritance, such as
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <stdio.h>
#include <tchar.h>
 
#include <iostream> 
using namespace std;
 
 
class B1
{
public: 
	virtual void SayHi()  { PrintClassName(); }
 
 
 
 
 
 
	virtual void PrintClassName() { cout << "This is B1."; }
};
 
class D1 : public B1
{
public:
	// No overridden functions at all
 
};
 
class D2 : public B1
{
public:
	virtual void PrintClassName() { cout << "This is D2."; }
};
 
int _tmain(int argc, _TCHAR* argv[])
{
	D1 d1;
	D2 d2;
 
	d1.SayHi();    // prints "This is B1"
	d2.SayHi();    // prints "This is D2"
 
	return 0;
}
2.ATL way of inheritance, such as
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <stdio.h>
#include <tchar.h>
 
#include <iostream> 
using namespace std;
 
 
template <class T>
class B1
{
public: 
	void SayHi() 
	{
		T* pT = static_cast<T*>(this);
		pT->PrintClassName();
	}
 
	void PrintClassName() { cout << "This is B1."; }
};
 
class D1 : public B1<D1>
{
public:
	// No overridden functions at all
 
};
 
class D2 : public B1<D2>
{
public:
	void PrintClassName() { cout << "This is D2."; }
};
 
int _tmain(int argc, _TCHAR* argv[])
{
	D1 d1;
	D2 d2;
 
	d1.SayHi();    // prints "This is B1"
	d2.SayHi();    // prints "This is D2"
 
	return 0;
}

This output (behavior) is the same.

Above, like an inheritance in the MFC, ATL is a like inheritance.
What is the difference between 1,2,
  • 1, using a virtual function. 2 is not used.
  • 1, the normal B1-> D1, D2 inheritance. 2, B1, and while the base, B1 template parameter, D1, D2 are using it.
.

In other words, we can achieve the same way that creates a virtual function with a template.
That is, 2, the virtual function table is not needed at run time, you reduce the consumption of the memory effect.

In addition, line 14 uses a static_cast.
That is, 2, and type checking at compile time, there are safer and more effective handling.

I inherited this, weight, and carried over to the weight, please imagine a more complex and when multiple inheritance. This, ATL / WTL secret is that it is a light one, I can imagine.

Referenced in the article, "Benefits of the technique" is reported.
  1. Object (ie it is dynamically assigned a virtual function) does not require a pointer.
  2. vtbls is no need, you can save as much memory.
  3. That has not been initialized with a NULL vtbl runtime virtual function call using the pointer is virtually impossible.
  4. We have decided to compile a function call at all, and they are already optimized.
vtbls: ※ please contact the virtual function table when I wrong reason.
Let's create a ATL window
So, immediately, a program to screen a simple dialog lets you create ATL and MFC.

Screen is displayed to the left to run,
"OK", "Cancel" to exit and click on
On the right of the screen, "Close" to exit and click the
Let's programming.

The dialog 1.MFC
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#define _ATL_CSTRING_EXPLICIT_CONSTRUCTORS
 
#define _AFX_ALL_WARNINGS
 
#include <afxwin.h>
#include <afxext.h>
 
#include <afxdtctl.h>
#ifndef _AFX_NO_AFXCMN_SUPPORT
#include <afxcmn.h>
#endif
 
 
#include "resource.h"
 
/////////////////////////////////////////
// The About dialog
 
class CAboutDlg : public CDialog
{
public:
	CAboutDlg() : CDialog(CAboutDlg::IDD) {};
 
	enum { IDD = IDD_ABOUT };
 
	protected:
	virtual void DoDataExchange(CDataExchange* pDX)
	{
		CDialog::DoDataExchange(pDX);
	}
 
protected:
	DECLARE_MESSAGE_MAP()
};
 
 
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()
 
 
 
 
 
 
 
 
 
/////////////////////////////////////////
//	WinMain
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hInstPrev,
                   LPSTR szCmdLine, int nCmdShow)
 
{
	if (AfxWinInit(hInst, hInstPrev, szCmdLine, nCmdShow)){
		CAboutDlg dlg;
		dlg.DoModal();
	}
 
	AfxWinTerm();
	return 0;
}
The dialog 2.ATL
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <atlbase.h>
#if _ATL_VER < 0x7000
extern CComModule _Module;
#endif
#include <atlwin.h>
 
#if _ATL_VER < 0x7000
CComModule _Module;
#define _APP _Module
#else
#define _APP _AtlBaseModule
#endif
 
#include "resource.h"
 
/////////////////////////////////////////
// The About dialog
 
class CAboutDlg : public CDialogImpl<CAboutDlg>
{
public:
    enum { IDD = IDD_ABOUT };
 
    BEGIN_MSG_MAP(CAboutDlg)
        MESSAGE_HANDLER(WM_CLOSE, OnClose)
        COMMAND_ID_HANDLER(IDOK, OnOKCancel)
        COMMAND_ID_HANDLER(IDCANCEL, OnOKCancel)
    END_MSG_MAP()
 
 
    LRESULT OnClose
        (UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
    {
        EndDialog(IDCANCEL);
        return 0;
    }
 
    LRESULT OnOKCancel
        (WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
    {
        EndDialog(wID);
        return 0;
    }
};
 
/////////////////////////////////////////
//	WinMain
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hInstPrev,
                   LPSTR szCmdLine, int nCmdShow)
{
#if _ATL_VER < 0x7000
    _Module.Init(NULL, hInst);
#endif
 
	CAboutDlg dlg;
	dlg.DoModal();
 
    _APP.Term();
    return 0;
}

Make a simple explanation.
ATL to DIALOG Class of the two.
CDialogImpl: The dialog uses a simple, CAxDialogImpl: ActiveX is used to have a dialog control.
Here is a simple dialog, CDialogImpl used.

Well, it felt like we saw. CAboutDlg slight differences in the process.
In MFC, Close, OnOK, OnCacel is easily available.
ATL, the default behavior is not around, you should write.
Little, SDK is reminiscent coding.
Also, _ATL_VER but here and there, which, ATL 7.0 for the basic structure of different earlier or later, we switched to using the flag description.
The free version provides all of ATL if, perhaps, ATL version, 3.0.
From various aspects, 3.0, MFC's programmer, I think there are many annoying situations. The biggest point is not more than 7.0 and ATL:: CString is not available. If possible, 7.0 is the version that is more than ready, and, if we make it easier.

I but not the same, in future, I think a little more掘RI下GETAI.

Finally, you have to compare the size.
Optimization / O1 in size results in the release
  • ATL 56kb
  • MFC 29kb (DLL)
  • MFC 136kb (STATIC)
Approximate, would result in the expected.

From above, some sites have a deeper comparison. In consideration of WTL, is a good reference.
http://www.endurasoft.com/vcd/mfcwtl.htm

Comments

Leave a Reply







  • はてなブックマークへ追加する
  • Facebookでシェアする
  • twitter でつぶやく
  • Google Plusでシェアする
  • Pocketでシェアする
ページトップへ