Constructor / destructor / assignment operator (a)

Constructor / destructor / assignment operator

Constructors,Destructors,and Assignment Operators

5. Understand C ++ member function default

Know what functions C++ silently writes and calls.

  1. C ++ member function default What?
    Here Insert Picture Description
  2. The default destructor is non-virtual, unless this class has virtual Bass class declares itself destructor.
  3. If the default copy constructor members need to copy the built type, it will be "a copy of the source object every bits" to complete the initialization. If the copy of the object is not built-in types (such as string type), then the copy will call the copy constructor string to complete.
  4. The default copy assignment will be completed copy of the object on the basis of the programmer does not violate the C ++ language features.
class Object{
public:
	Object(string& s,const T& value);
	string& s;
	const T object_value; 
};
......
std::string str1("Hello");
srd::string str2("Gun");
Object<std::string,int> o1(str1,666);
Object<std::string,int> o2(str2,438);
o1=o2;//调用默认的赋值运算符是不会得逞的。

The string & members o1 is reference, can not be changed to point to different objects, members having const const T o1 the property, can not be changed.

In addition to the constructor, the remaining members of a function call is required to use the implied this pointer to complete the call.

About the default C ++ member functions related to knowledge, recommended reading: http: //c.zhizuobiao.com/c-19012100036/

6. If you do not want to use the default compiler-generated functions in respect explicitly rejected

Explicitly disallow the use of compiler-generated functions you do not want.

  1. The default function declaration privatization (private type function), and do not define (preventing friend function call, not directly implement).
#include<iostream>
class No_copy {
private:
	No_copy(No_copy&);
	No_copy& operator=(const No_copy&);
};

Copy constructor and assignment operator overloading object itself visible, a friend function is visible, but I did not realize the function can not be called.
2.

class No_copy{
protected:
	No_copy (){}
	~No_copy (){}
private:
	No_copy (const No_copy &);
	No_copy& operator=(const No_copy &);
};
 
class No_copy_son:private No_copy 
{
	.......
};

A derived class can not call the base class's private member function, or a copy of the assignment can not be achieved.
3. C ++ 11 is too humane, no ears.

#include<iostream>
class No_copy {
private:
	No_copy(No_copy&)=delete;
	No_copy operator=(const No_copy&)=delete;
};

7. polymorphic base class declaration virtual destructor

Declare destructors virtual in polymorphic base classes.

  1. Preferably virtual destructor function. In the multi-state scenario, if the destructor is not virtual, by deleting the base class derived class pointer, the result will be undefined, usually derived class is not destroyed. And in destroying the base class objects to form a resource leaks and other problems.
#include<iostream>
class Father {
public:
	Father()
	{	
		father_ptr = new char[10];
		std::cout << "Father()" << std::endl;
	}
	~Father()
	{	
		delete[] father_ptr;
		std::cout << "~Father()" << std::endl;
	}
private:
	char* father_ptr;
};
class Son :public Father
{
public:
	Son()
	{
		son_ptr = new char[5];
		std::cout << "Son()" << std::endl;
	}
	~Son()
	{
		delete[] son_ptr;
		std::cout << "~Son()" << std::endl;
	}
private:
	char* son_ptr;
};

int main()
{
	Father* pp = new Son;
	delete pp;
}
Father()
Son()
~Father()

F:\代码\Project1\Debug\Project1.exe (进程 10520)已退出,返回代码为: 0。
若要在调试停止时自动关闭控制台,请启用“工具”->“选项”->“调试”->“调试停止时自动关闭控制台”。
按任意键关闭此窗口...

Destructor are not defined as a virtual function, resulting in a memory leak.

  1. Only when the class comprising at least one virtual function, it was declared virtual destructor. classes is not designed to be used as Base classes, or not to have the polymorphism, which will not declare virtual destructor (vptr increases to increase the object size).
  2. The STL string, vector, list and other containers are non virtual destructor, do not attempt to make such containers as a base class.
  3. Sometimes makes class with a pure virtual destructor, it may be quite convenient. Preferably pure virtual destructor is defined as derived class destructor calls the destructor of the base.

8. Do not let escape abnormal destructor

Prevent exception from destructors.

  1. If the destructor throws an exception occurs prematurely ended or ambiguous behavior. Not like C ++ destructor throw an exception.
  2. If the destructor must perform an action, and this action may throw an exception on failure. Then the best chance of giving the customer an exception handling. And if after treatment "because of the abnormal operation occurred" in the client still can not avoid the exception, in the end only "forced end of the program," or "abnormal swallow."
class DBConn{
public:
	void close()
	{
		db.close();
		closed=true;
	}
	~DBConn()
	{
		if(!closed){
			try{
				db.close();
			}
			catch(...){
				//abort()强迫结束程序,可抢先制止不明确行为
				//或者记录异常行为吞下异常
			}			
		}
	}
private:
	DBConnection db;
	bool closed;
};
  • If the customer needs to run an abnormal function during an operation to respond thrown, then the class should provide a common function (rather than in the destructor) to perform the operation.
Published 139 original articles · won praise 55 · views 60000 +

Guess you like

Origin blog.csdn.net/Vickers_xiaowei/article/details/102363912