Get the App
SLTechnology News&Howtos  ›  Servers  › 

WxWidgets lesson 2 DECLARE_NO_COPY_CLASS forbids copying of classes

Shulou Source: shulou.com Published: 2022-06-03 02:59:07 09月17日 Update

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.

Tags: Function variable file copy important consistent code parameter actual object member equal sign class information type operator error support private compilation operation Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Microsoft MariaDB Xiaomi Docker