tags:

views:

208

answers:

5
#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 is 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()

Is 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 have, what function we can override...

Following is the head file:

/*----------------------------------------------------------------------
Copyright (C)2001 MJSoft. All Rights Reserved.
This source may be used freely as long as it is not sold for
profit and this copyright information is not altered or removed.
Visit the web-site at www.mjsoft.co.uk
e-mail comments to [email protected] 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

Thanks in advance!!!

+3  A: 

Question 2: those are not functions. It is an initializer list 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 the copy constructor will be invoked instead of 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 declaration of the member variables.

Peter Mortensen
Thanks, and how is the others ?
MemoryLeak
Sorry, I can not answer all of them and question 1 and question 3 have already been answered by others.
Peter Mortensen
MemoryLeak
+6  A: 

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

jcopenha
+2  A: 

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 (i.e. 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.

jscharf
+2  A: 

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.

ChrisW
Wow, amazing, thanks for your answer!!!
MemoryLeak
+1  A: 

When should my destructor be virtual?

http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.7

DanM