An act with a neutral meaning can be interpreted differently in the eyes of different people. Like you help school sister open the water, if she likes you, that you are gentle and intimate warm men in the winter, if she does not like you, that you are on the roadside romantic funny than.

By analogy with C++, it automatically prepares you without your knowledge a set of basic class member methods, such as ① no-argument constructors, ② empty destructors, ③ copy constructors, and ④ assignment operator functions. Do you think this is a kind of tenderness, or a kind of love?

There’s no right answer, and it depends on the needs of the class you’re designing. In most cases, the above auto-generated class member methods are essential because they provide the bare minimum of a class object’s properties. But on special occasions, they are the things we want to get rid of.

For example, if you design a class that represents a student, look like this

class student

{

**int ID; // Id **

string name; / / name

*char data; // Other information

};

If we think that the above four auto-generated class methods are sentimental, let’s take a look at how to avoid and reject these roadside pranks.

First of all, we assume that any student object must have an ID and a name, so when defining a student object, we should reject C++ ‘s arbitrary creation of the parameterless constructor. This is easy. We can just define any version of the class constructor and gracefully reject it.

Gracefully reject the C++ auto-generated ① no-argument constructor by defining a constructor

student::student(const int &id, const string &name)

{

ID = id;

this->name = name;

data = new char[100];

}

Second, we believe that any student object should release the resources it holds when it exits its scope. Therefore, like the constructor, we can define a destructor to reject the empty destructor generated by the system automatically.


Gracefully reject the ② empty destructor that C++ automatically generates by custom destructors

student::~student( )

{

delete [ ] data;

}

Again, we don’t want to use one student to initialize another student, nor do we want direct assignments of two student objects. After all, no two people are exactly alike (even identical twins are not allowed). In other words, we want the following code to be illegal:

student Jack(1234, “Jack”);

student Rose(Jack);

student Mike = Jack;

A closer look at the code above shows that Rose(Jack) actually calls Rose’s default copy constructor and Mike = Jack actually calls Mike’s default assignment operator, emmmm… . What if we made these two funny functions private? That’s right! In this case, the above code is illegal.

Gracefully rejects C++ auto-generating ③ copy constructor and ④ assignment operator functions via private permissions

class student

{

. .

private :

student(const student &);

student &operator=(const student &);

};

This is not a trick I invented. It’s actually a trick that’s common to both the official C++ standard and Boost libraries. Further, we can define a special class Uncopyable that contains the private copy constructor and private assignment operator functions. And make it the base class for all classes that need to reject these default class member methods.

A magic base class that seals the ③ copy constructor and ④ assignment operator functions inside private permissions

class Uncopyable

{

protected:

Uncopyable(); // Allow derived classes to call constructors

~Uncopyable();

private:

Uncopyable(const Uncopyable &); // Block derived classes

Uncopyable &operator=(const Uncopyable &);

};

Next, we just need the Student class to inherit Uncopyable

class student : public Uncopyable

{

. .

};

At this point! We’ll have the perfect rejection of C++! Oh yeah!