What is the pod type?

Source: Internet
Author: User
Source: msdn magazine November 2004 (C ++ Q &)

  1. Call virtual functions
  2. Continuous view status
  3. Pod type concept

Problem:In C ++, the derived virtual function cannot be called from the constructor of a class, because the virtual table has not yet been fully created. But in C #, it seems like we can. Is that true? Why is there such a difference?
Clifton F. VaughnAnswer:Indeed, C # And C ++ are different in this respect. In C ++, if you call a virtual function from a constructor or destructor, the virtual function called by the compiler is defined in the currently constructed class instance (for example, if base: base is called from base: somew.fn), it is not the underlying derived instance (the most derived instance), as you said, because the virtual table has not been fully initialized before the execution of the underlying derived constructor. Another saying is that the derived class has not been created yet.


Figure 2 virtual function testsimilarly

When you call a virtual function from a destructor, C ++ calls the destructor of the base class because the derived class has been destroyed (The Destructor has been called ). Although this behavior can cause abnormal results (this is why calling a virtual function from a constructor or destructor is considered a bad Programming Practice ), it is the basic knowledge that most C ++ programmers must understand.
As you have pointed out, C # is different. Managed object-whether in C #, hosting C ++, or any other. net compatible language-it is created as its final type. That is to say, if you call a virtual function from the constructor or destructor, the system calls the function derived from the last layer. The program shown in figure 1 illustrates this. If you compile and run this program, you will see the output shown in figure 2.
This behavior seems odd for C ++ programmers. It means that before the derived class is initialized, you can call a virtual function of a derived type -- that is, before its constructor runs. Similarly, if you call a virtual function from a base class destructor, the function runs after the derived class is destroyed-that is, after the Destructor is called. So let's not talk about the reason for this difference. I didn't say that calling a virtual function from the constructor/destructor is considered a bad practice.
Why should Microsoft's team design C # like this? It simplifies memory management. In order to release the memory, the garbage collector needs to know the size of the object. If C # constructs an object like C ++, you may encounter the following situation: there are two objects, obj1 and obj2. The following two statements are true:

typeof(Obj1)==typeof(Obj2) sizeof(Obj1)!= sizeof(Obj2) 

Because one object is partially constructed. (Do not forget that the Garbage Collector runs asynchronously .) By constructing an object as the final type, the garbage collector can determine the object size from its type. If C # is partially constructed like C ++, the Garbage Collector requires more code to determine the actual size of some constructed objects. This will bring complexity and performance degradation. First, it is very frustrating to solve this problem. So for the sake of fast garbage collection benefits, Microsoft's team decided to implement C # as above #. For more information, see Raymond Chen's blog "the old New thing ".

Problem:In the 2004 column, you showed how to change the latest view status settings in the file opening dialog box, but it does not involve saving the latest view settings used by this user. The problem I encountered was to read the user's existing file opening dialog box settings. I only found the method to directly read the information in the list box, but when the user chooses the thumbnail mode, the correct information cannot be obtained. Are you sure you want to solve this problem?

Maarten van DillenProblem:I am using the common cfiledialog class for development. It should not be difficult, but it does not seem like that. The view mode of the force file open dialog box is thumbnail. I want to use VISUAL C ++. Can you provide some suggestions? Elliot Leonard

Answer:Several readers are asking the thumbnails in the file opening dialog box. In my March column, I demonstrated how to send the wm_command message to the shelldll_defview special window in the file opening dialog box to set different view Modes -- but how do you know which mode is the current one? You must obtain the list control and call clistctrl: getview:

// in dialog classHWND hlc = ::FindWindowEx(m_hWnd, NULL, _T("SysListView32"), NULL);CListCtrl* plc = (CListCtrl*)CWnd::FromHandle(hlc);DWORD dwView = plc->GetView();

Clistctrl: getview returns one of the lv_xxx codes, but as Maarten finds, Windows returns lv_view_icon in both the icon and thumbnail modes.
So how can we tell which view mode is used? I racked my brains and got into the header file to find a message called lvm_getitemspacing. What is the purpose of this message? It is used to get the icon interval. As the name suggests, the icon interval is the pixel interval between icons in the icon view mode. Lvm_getitemspacing is not very easy to use, so that MFC does not wrap it (for example, there is no clistctrl: geticonspacing function in MFC ). Therefore, in MFC, you have to send the message by yourself:

CSize sz = CSize(plc->SendMessage(LVM_GETITEMSPACING));

Windows returns the size in the usual way, the CX/CY encoded in the high and low-level characters, and then the csize is politely decoded for you. Once there is an icon interval, you can compare it with the system interval value returned by getsystemmetrics (sm_cxiconspacing. If the icon interval of the list view is the same as that of the system, the view is the icon mode. If the value is greater than the system interval, the view is in the thumbnail mode:

if (sz.cx > GetSystemMetrics(SM_CXICONSPACING)) {    // thumbnail view} else {   // icon view }

After talking about so many thumbnails, the next question is how to continue the view status of different user sessions? In this case, when the program is terminated, you need to use the profile function to save the last used mode in the user configuration file and restore it again when the program is started the next time. I wrote a small demo program, dlgtest. The program uses a class cpersistopendlg that implements continuous program behavior. This class uses another clistviewshellwnd class to encapsulate the shelldll_defview window (see the March column ). Clistviewshellwnd contains functions for obtaining and setting the view mode. These functions are used to distinguish between the icon mode and the thumbnail mode:

CListViewShellWnd m_wndLVSW;...m_wndLVSW.SetViewMode(ODM_VIEW_THUMBS);

The ondestroy processor of clistviewshellwnd saves the view mode in m_lastviewmode, a data member. When the dialog box is destroyed, the cpersistopendlg destructor calls writeprofileint to write this value to the user configuration file. When the dialog box is started, cpersistopendlg sends an initialization message to itself. This message processing routine calls getprofileint to read the values stored in the configuration file from the disk and set the view mode. Postmessage must be called because the general initialization messages wm_initdialog and cdn_initdone will arrive before the file dialog box is fully initialized. For more information, see the March column.
By the way, you should use getprofilexxx and writeprofilexxx to continuously set the application. MFC uses cwinapp to encapsulate these functions. If you call cmyapp: setregistrykey ("keyname") when the application is started (usually in the initinstance function), MFC uses the Registry to store user configuration information, rather than INI files. The INI file used by dlgtest is as follows:

 

[settings]ViewMode=28717

Problem:Occasionally, the term "pod type" is displayed in some text materials, C ++ documents, and Microsoft. NET frameworks ". What does this term mean?
Shelby nagwitzAnswer:You can think of the pod type as a data type packaged with a green protective layer from outer space. The pod means "plain old data, that's "Full old data !) This is the meaning of the pod type. Its exact definition is quite rough (see the C ++ ISO standard). It basically means that the pod type contains raw data compatible with C. For example, the structure and integer types are pod type, but the classes with constructors or virtual functions are not. The pod type does not have virtual functions, base classes, user-defined constructor, copy constructor, value assignment operator, or destructor.
To conceptual the pod types, you can copy them by copying their bits. In addition, the pod type can be non-initialized. For example:

struct RECT r; // value undefinedPOINT *ppoints = new POINT[100]; // dittoCString s; // calls ctor ==> not POD

Initialization is usually required for non-pod types, whether it is to call the default constructor (provided by the compiler) or self-written constructor.
In the past, pods were important for developers who wrote compilers or C-Compatible C ++ programs. Now, the pod comes to the. NET environment. In managed C ++, managed types (including _ value and _ GC) can contain embedded native pod types. Figure 3 shows the sample code. Managed circle classes can contain point but cannot contain cpoint classes. If you try to compile pod. CPP will report a c3633 error: "cannot define ''m _ center'' as a member of managed ''circle'' because of the presence of default constructor ''cpoint :: cpoint ''on class ''cpoint ''. "(TRANSLATOR: Because the class cpoint has the default constructor 'cpoint: cpoint', you cannot define'm _ Center' as a member of the Managed class 'circle)
. Net limits that embedded local objects can only be of the pod type. The reason is that they can be copied securely without worrying about calling constructors and initializing virtual tables, or any other mechanism not required by the pod type.

Ask Paul a question and comment and send it to cppqa@microsoft.com.
  Author Profile
  Paul dilasciaHe is a freelance writer, consultant, and Web/UI Designer. He is the author of the writing reusable windows code in C ++ book (Addison-Wesley, 1992. Learn more through http://www.dilascia.com. This article is from the msdn magazine November 2004 journal and can be obtained through a local newsstand, or preferably subscribed
This article is translated by vckbase MTT

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.