简体   繁体   中英

What do these C++ code snippets do?

#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 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's this used for?

virtual ~CSortHeaderCtrl();

Why set the destructor function to be virtual?

void CSortHeaderCtrl::Serialize( CArchive& ar )

When will this function be called?

Is this extended from parent?

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

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

The following 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

Question 1: The DEBUG_NEW is probably so the 'new' operator records some extra information about where and when a block was allocated to help in detecting memory leaks, see this . The THIS_FILE[] static char array simple holds the current filename, probably used by the debug 'new'

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

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

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

m_iSortColumn = -1;
m_bSortAscending = TRUE;

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

Note that, with an initializer list, the initialization order is not determined by the order it is written, but by order of the class inheritance and by order of declaration of the member variables.

First of all, CSortHeaderCtrl has a virtual destructor because in C++ it is proper practice to make destructors virtual.

Destructors are made virtual in base classes because it means that the destructors in classes derived from the base will be called.

If destructors in derived classes aren't called (ie the base class destructor is non-virtual), then they will most likely leak memory and leave resources (streams, handles, etc) open.

The rest of the code you posted is generated by Visual Studio to handle common or redundant MFC tasks for you, for example mapping Win32 messages to member functions of your class or window. You shouldn't touch this code, as it is likely to be overriden or you will break it and have a debugging related headache coming your way.

Why define these tags ?

See jcopenha's answer.

What is the two functions after colon used for ?

See Peter's answer.

Is there any similar things in C# like this ? What's this used for ?

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

It's called a "message map" (probably described in one of the subsections of MFC Library Reference Message Handling and Mapping ).

Its contents are typically created/edited via the IDE "Class Wizard" (not edited manually using the code/text editor).

Why set the destructor function to be virtual ?

In C++, if a class might be subclassed then its destructor should almost always be virtual (because otherwise if it's not virtual and you invoke it by deleting a pointer to the superclass, the destructor of the subclass wouldn't be invoked).

When will this function be called ?

That's probably described here: MFC Library Reference Serialization in MFC .

is this extended from parent?

Acording to that link I just gave above, it's the CObject ancestor class: "MFC supplies built-in support for serialization in the class CObject. Thus, all classes derived from CObject can take advantage of CObject's serialization protocol."

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

The MFC reference documentation.

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

You can typically override everything that virtual and not private. I think you can also/instead use the Class Wizard that's built-in to the IDE.

CSortHeaderCtrl is apparently a 3rd-party class, though, not a Microsoft class. Perhaps it's authors/vendor wrote some documentation for it, if you're supposed to be using it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM