If the object you want to define for your class is unique, that is, the object cannot be copied, or assigned to another object using an assignment operation, the best way is to disable the copy constructor and the assignment operator. Several disabled methods are described below. (method from effective C + +, similar to if you want to disable other functions of the class)
1. Defined as private and does not implement it
As we know, the copy constructor and the assignment operator overload function, even if not defined, the compiler generates a default function. However, if defined, the class will still support both functions. So how do we disable them?
In C + +, if you don't want an object to call a method, you can declare the method private, so we can declare the copy constructor and the assignment operator overload function as private, which prevents others from invoking it. However, the private function can also be called in the class's member functions and in the friend function. The trick here is to define the function as private and not implement it.
class Specialclass { ... Private : Specialclass (const specialclass&); Specialclassoperator= (const specialclass&); };
By defining the specialclass as such and not implementing the two functions, if someone tries to copy the object of this class, the compiler will error:
Specialclass SC1; Specialclass SC2 (SC1); // Error , compilation error = SC1; // Error , compilation error
And even if it is called in the member function or in the friend function, the link also goes wrong.
2. Inheriting the Uncopyable class
The first method solves the problem of disabling the copy constructor, but it is also imperfect that if the user accidentally calls it in a member function or a friend function, then the error can only be found at the link stage. So there's a way to inherit a uncopyable class:
class uncopyable {protected: uncopyable () {} ~uncopyable () {}private : uncopyable (const uncopyable&); Uncopyableoperator= (const uncopyable&);};
At this point, you only need to inherit the Uncopyable class to prevent its objects from being copied. The principle is: Specialclass inherited the Uncopyable function, its object if the copy constructor is called, or use the assignment operator, then compile, will call the corresponding function of the base class, but the base class of the two functions are private, so the compilation will fail.
In addition, boost also provides a uncopyable function, the class name is noncopyable, so we can do the same:
#include <boost/utility.hpp>class specialclass:boost::noncopyable { ...};
3. New methods in c++0x
In c++0x, two new keywords were added: Default and delete, which can be used to control a class using the default function or disable a function (delete). Where you can define a function as the default function by using the DEFAULT keyword. Such as:
class Specialclass { ... Public : Specialclass (constdefault; Specialclassoperator= (constdefault; };
This may be a bit redundant, but it can be a visual representation of "The default function I'm using". Also, if someone wants to explicitly define a default function, but the code they write may have errors, you can use this keyword conveniently and without error.
Delete keyword can disable the method of class, as we did in the first two sections above, we can use this one keyword to complete:
class Specialclass { ... Public : Specialclass (constDelete; // Disable copy Constructors operator= (constdelete; };
The default keyword can be used for all functions that contain the defaults. The DELETE keyword can be used for all methods of a class, such as certain types of parameters that can disable a function:
class Specialclass { ... Public : void fn (long); void fn (intdelete;}; Specialclass SC; Long L;sc.fn (l); // OK, call void FN (long); int I;sc.fn (i); // error, calling void fn (int);
The above code disables a function of type void fn (int) and, if not disabled, SC.FN (i) implicitly converts the call to the void FN (long) function.
Reference:
C++11 FAQ
The article was excerpted from Xd_xiaoxin's blog post.
Disable compiler auto-generated functions (effective C + + 06)