What are these C ++ code snippets doing?

#ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
#endif

      

Why define these tags?

CSortHeaderCtrl::CSortHeaderCtrl()
    : m_iSortColumn( -1 )
    , m_bSortAscending( TRUE )
{
}

      

What are the two functions after the colon used for?

BEGIN_MESSAGE_MAP(CSortHeaderCtrl, CHeaderCtrl)
    //{{AFX_MSG_MAP(CSortHeaderCtrl)
        // NOTE - the ClassWizard will add and remove mapping macros here.
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

      

Are there any similar things in C # like this?

What is it used for?

virtual ~CSortHeaderCtrl();

      

Why set up a virtual destructor function?

void CSortHeaderCtrl::Serialize( CArchive& ar )

      

When is this function called?

Is this extended from the parent?

By the way, when you want to extend the MFC class, which document will you read?

Since we don't know what function it has, what function can we override?

Below is the header file:

/* File: SortHeaderCtrl.h 

   Purpose:  Provides the header control, with drawing of
             the arrows, for the list control.
*/

#ifndef SORTHEADERCTRL_H
#define SORTHEADERCTRL_H

#if _MSC_VER > 1000
    #pragma once
#endif // _MSC_VER > 1000

class CSortHeaderCtrl : public
CHeaderCtrl { // Construction public:
    CSortHeaderCtrl();

    // Attributes public:

    // Operations public:

    // Overrides     // ClassWizard generated
    virtual function overrides
        //{{AFX_VIRTUAL(CSortHeaderCtrl)
        public:     virtual void Serialize(CArchive& ar);
        //}}AFX_VIRTUAL

    // Implementation public:     virtual
    ~CSortHeaderCtrl();

    void SetSortArrow( 
        const int iColumn,
        const BOOL bAscending );

        // Generated message map functions
    protected:     
        void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct );

        int m_iSortColumn;     
        BOOL m_bSortAscending;

        //{{AFX_MSG(CSortHeaderCtrl)         //
        NOTE - the ClassWizard will add and
        remove member functions here.
        //}}AFX_MSG

    DECLARE_MESSAGE_MAP() };

//{{AFX_INSERT_LOCATION}} // Microsoft
Visual C++ will insert additional
declarations immediately before the
previous line.

#endif // SORTHEADERCTRL_H

      

+2


source to share


5 answers


Why define these tags?

See jcopenha's answer.

What are the two functions after the colon used for?

See Peter's answer.

Are there any similar things in C # like this? What is it used for?

In C #, it can be implemented as a dictionary of delegates.

He called it a "message map" (possibly described in one of the MFC Library Reference Handling and Mapping subsections ).

Its content is usually created / edited through the Class Wizard IDE (not manually edited with a code / text editor).

Why set up a virtual destructor function?

In C ++, if a class can be subclassed, its destructor must almost always be virtual (because otherwise, if it is not virtual and you call it by removing the pointer to the superclass, the subclass's destructor will not be called).



When is this function called?

This is probably documented here: Serializing MFC Library Links to MFC .

is this an extension from the parent?

According to the link I just gave above, this is the CObject ancestor class: "MFC provides built-in support for serialization in the CObject class. Thus, all classes derived from CObject can use the CObject serialization protocol."

By the way, when you want to extend the MFC class, which document will you read?

MFC reference documentation.

Since we don't know what function it has, what function we can override ...

You can usually override all virtual ones, not private ones. I think you can also use the class wizard that is built into the development environment.

CSortHeaderCtrl appears to be a third party class, but not a Microsoft class. Perhaps the author / provider wrote some documentation for it if you should use it.

+2


source


Question 1: DEBUG_NEW is probably so the "new" statement writes additional information about where and when the block was allocated to help in detecting memory leaks, see this . The THIS_FILE [] static char simple array contains the current filename, probably used by the debug "new"

Question 2: This is a C ++ initialization list.



Question 3: The destructor is declared virtual because other virtual members exist and it is a derived class. The "delete" operator needs to know the correct size of the object it is deleting, along with which the actual handle to call, see this

+6


source


As for question 2: these are not functions. They are initializer lists for CSortHeaderCtrl members. You can think of it as equivalent:

m_iSortColumn = -1;
m_bSortAscending = TRUE;

      

I emphasize "think about it" because for members that are classes, only the copy constructor will be called (instead of the first copy and then the assignment operator).

Note that in the initializer list, the order of initialization is not determined by the order of its writing, but by the order of class inheritance and by the order of the declaration of the member variables.

+3


source


First of all, it CSortHeaderCtrl

has a virtual destructor, because in C ++ the correct practice is to make destructors virtual.

Destructors are created virtual in base classes because that means that destructors in base-derived classes will be called.

If destructors in derived classes are not called (i.e. the base class destructor is not virtual), then most likely they will be memory leaks and resources (threads, handles, etc.) will remain.

The rest of the code you submit is generated by Visual Studio to handle common or redundant MFC tasks for you, such as mapping Win32 messages to member functions of your class or window. You shouldn't touch this code as it will likely be overkill or you will break it and your headache will be debugged on your way.

+2


source


+1


source







All Articles