WxWidgets lesson 2 DECLARE_NO_COPY_CLASS forbids copying of classes
Description
In the defs.h file, the macro is defined as follows
# define DECLARE_NO_COPY_CLASS (classname)\
WxDECLARE_NO_COPY_CLASS (classname)
The actual definition of wxDECLARE_NO_COPY_CLASS is as follows:
# define wxDECLARE_NO_COPY_CLASS (classname)\
Private:\
Classname (const classname&);\
Classname& operator= (const classname&)
From the expansion of the macro definition above, the copy constructor and equal sign operation operator of the class are set to private member functions, which prohibits any copy outside the class.
Usage
Class BaseFrame
{
DECLARE_NO_COPY_CLASS (BaseFrame)
Public:
BaseFrame ()
Virtual ~ BaseFrame ()
}
Expand as follows:
Class BaseFrame
{
Private:
BaseFrame (const BaseFrame&)
BaseFrame& operator= (const BaseFrame&)
Public:
BaseFrame ()
Virtual ~ BaseFrame ()
}
Be careful
In the header file, the variable name in the parameter list of the function is not important and may not be written. The declaration of the function can be different from the list variable name of the implementation, but the type of the variable must be the same.
IMPLEMENT_DYNAMIC_CLASS
Code
# define IMPLEMENT_DYNAMIC_CLASS (NMagneb) wxIMPLEMENT_DYNAMIC_CLASS (nMagneb)
/ / Single inheritance with one base class
# define wxIMPLEMENT_DYNAMIC_CLASS (name, basename)\
WxIMPLEMENT_CLASS_COMMON1 (name, basename, name::wxCreateObject)\
WxObject* name::wxCreateObject ()\
{return new name;}
Description
Support for run-time class information is mainly to determine whether an object belongs to or is derived from the implementation of a particular class
This macro definition will cause compilation errors when introducing windows.h header files, so it needs to be used with caution.