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.


Add to your favorites(bookmarks): はてなブックマークへ追加するdel.icio.usLivedoor ClipYahoo!FC2Nifty ClipPOOKMARK. AirlinesBuzzurl(バザール)Choixnewsing

Trackback URL

After Admin approves this comment, it will be shown.


Comments

Leave a Reply





*





Tag Cloud

Links

Site Description

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

Add to your favorites(bookmarks)

はてなブックマークへ追加するdel.icio.usLivedoor ClipYahoo!FC2Nifty ClipPOOKMARK. AirlinesBuzzurl(バザール)Choixnewsing