1. 程式人生 > >取消預設構造,拷貝構造和賦值構造的巨集定義

取消預設構造,拷貝構造和賦值構造的巨集定義

巨集定義如下:

// Put this in the declarations for a class to be unassignable.
#define RTC_DISALLOW_ASSIGN(TypeName) \
  void operator=(const TypeName&) = delete

// A macro to disallow the copy constructor and operator= functions. This should
// be used in the declarations for a class.
#define RTC_DISALLOW_COPY_AND_ASSIGN(TypeName) \
  TypeName(const TypeName&) = delete;          \
  RTC_DISALLOW_ASSIGN(TypeName)

// A macro to disallow all the implicit constructors, namely the default
// constructor, copy constructor and operator= functions.
//
// This should be used in the declarations for a class that wants to prevent
// anyone from instantiating it. This is especially useful for classes
// containing only static methods.
#define RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \
  TypeName() = delete;                               \
  RTC_DISALLOW_COPY_AND_ASSIGN(TypeName)

可見,

(1)取消了預設構造,即取消了拷貝和賦值構造;

(2)取消了拷貝構造,即取消了賦值構造.

使用方法如下:

class RTPSender{
  RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(RTPSender);
};

可以取消RTPSender的預設構造,預設拷貝構造和預設的賦值建構函式.