Two useful macros: "Disallow copying of class members" and "Disallow implicit construction"

1. The core of prohibiting the copying of class members is that copying functions, including "copy constructor" and "operator = overloading", are not allowed to be seen outside the class.

#define DISALLOW_COPY_AND_ASSIGN(TypeName) \

private:                     \

TypeName(const TypeName&);               \

TypeName& operator=(const TypeName&)

2. If implicit construction is prohibited, the default constructor can be hidden. In most compilers, the explicit keyword can also be added to the constructor to avoid implicit construction.

#define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \

private:                     \

TypeName();                                    \

DISALLOW_COPY_AND_ASSIGN(TypeName)

For more explanation, see "More Effective C++"

3. The boost::noncopyable class that can inherit the Boost library implements similar functions.

 

{{o.name}}
{{m.name}}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324205311&siteId=291194637