您的位置:首页 > 其它

constructors and destructors

2020-05-11 04:08 561 查看

对文档不作任何解释说明,花开两朵,各表一枝

#Constructors

A constructor is a kind of member function that initializes an instance of its class. A constructor has the same name as the class and no return value. A constructor can have any number of parameters and a class may have any number of overloaded constructors. Constructors may have any accessibility, public, protected or private. If you don’t define any constructors, the compiler will generate a default constructor that takes no parameters; you can override this behavior by declaring a default constructor as deleted.

1.构造函数是初始化对象的一个成员函数.
2.构造函数与类同名且无返回值.
3.允许以不同的参数列表重载
4.可以允许不同的访问方式.
5.无定义则由编译器自动生成一个无参的默认构造,当重写(定义)时,默认的将自动销毁.
Order of Construction
•It calls base class and member constructors in the order of declaration.
•If the class is derived from virtual base classes, it initializes the object's virtual base pointers.
•If the class has or inherits virtual functions, it initializes the object's virtual function pointers. Virtual function pointers point to the class's virtual function table to enable correct binding of virtual function calls to code.
•It executes any code in its function body.
1.顺序调用基类成员的构造函数、基类构造函数.
2.如果当前类是重生类,则要初始化该对象的虚基指针.
3.如果当前类拥有(定义/继承)虚函数,则要初始化该对象的虚函数指针,该指针指向该类的虚函数表(table)以确保虚函数正确绑定调用代码.
The following example shows:

class Contained1 {
public:
Contained1() { cout << "Contained1 constructor." << endl; }
};

class Contained2 {
public:
Contained2() { cout << "Contained2 constructor." << endl; }
};

class Contained3 {
public:
Contained3() { cout << "Contained3 constructor." << endl; }
};

class BaseContainer {
public:
BaseContainer() { cout << "BaseContainer constructor." << endl; }
private:
Contained1 c1;
Contained2 c2;
};

class DerivedContainer : public BaseContainer {
public:
DerivedContainer() : BaseContainer() { cout << "DerivedContainer constructor." << endl;}
private:
Contained3 c3;
};

int main() {
DerivedContainer dc;
int x = 3;
}
Here's the output:
Contained1 constructor.
Contained2 constructor.
BaseContainer constructor.
Contained3 constructor.
DerivedContainer constructor.

这里将不解释Member ListsExplicit ConstructorsDefault ConstructorsCopy and Move ConstructorsVirtual Functions in ConstructorsDelegating Constructors etc.

#Destructor

Destructor” functions are the inverse of constructor functions. They are called when objects are destroyed (deallocated). Designate a function as a class’s destructor by preceding the class name with a tilde (~). For example, the destructor for class String is declared: ~String(). The destructor is commonly used to “clean up” when an object is no longer necessary.

1. 析构函数是相对于构造函数的反转
2. 当对象销毁的时候调用(delete/free)
3. 类名前加波浪线(~)
Delcaring destructors
•Do not accept arguments.
•Cannot specify any return type (including void).
•Cannot return a value using the return statement.
•Cannot be declared as const, volatile, or static. However, they can be invoked for the destruction of objects declared as const, volatile, or static.
•Can be declared as virtual. Using virtual destructors, you can destroy objects without knowing their type — the correct destructor for the object is invoked using the virtual function mechanism. Note that destructors can also be declared as pure virtual functions for abstract classes
Using destructors
•An object allocated using the new operator is explicitly deallocated using the delete operator. When objects are deallocated using the delete operator, memory is freed for the "most derived object," or the object that is a complete object and not a subobject representing a base class. This "most-derived object" deallocation is guaranteed to work only with virtual destructors. Deallocation may fail in multiple-inheritance situations where the type information does not correspond to the underlying type of the actual object.
•A local (automatic) object with block scope goes out of scope.
•The lifetime of a temporary object ends.
•A program ends and global or static objects exist.
•The destructor is explicitly called using the destructor function's fully qualified name. (For more information, see Explicit Destructor Calls.)

According to the different mode of Memery allocation:(stack/heap/other)
1.堆式分配显式兮(new/delete), 释放之于”派生项”,实体也。故与虚析构为伍,且忌多继承之顺序
2.栈式随域而安
3.(其他分配方式)全局或静态对象退出则析构

The thumbnail of ARM, just for reference.

#FAQ

Why don’t we have virtual constructors?

“A virtual call is a mechanism to get work done given partial information. In particular, “virtual” allows us to call a function knowing only an interfaces and not the exact type of the object. To create an object you need complete information. In particular, you need to know the exact type of what you want to create. Consequently, a “call to a constructor” cannot be virtual. “ —— Bjarne Stroustrup , Modified September 30, 2017

直译:老爷子曰: 虚调用就是只要给出部分信息就能完成工作的一种机制。virtual说明符只用在已知接口而非一个具体对象;而创建一个对象你要知道全部信息尤其你要知道你要创建出个什么鬼。
Why are destructors not virtual by default?

“Because many classes are not designed to be used as base classes. Virtual functions make sense only in classes meant to act as interfaces to objects of derived classes (typically allocated on a heap and accessed through pointers or references). So when should I declare a destructor virtual? Whenever the class has at least one virtual function. Having virtual functions indicate that a class is meant to act as an interface to derived classes, and when it is, an object of a derived class may be destroyed through a pointer to the base.” —– Bjarne Stroustrup , Modified September 30, 2017

直译:老爷子曰: 多数类作为普通类无虚函无派生,故无需通过指向父类指针销毁。
Virtual destructors
class Base {
public:
virtual ~Base() { /* releases Base's resources */ }
};

class Derived : public Base {
~Derived() { /* releases Derived's resources */ }
};

int main()
{
Base* b = new Derived;
delete b; // Makes a virtual function call to Base::~Base()
// since it is virtual, it calls Derived::~Derived() which can
// release resources of the derived class, and then calls
// Base::~Base() following the usual order of destruction
}
PANHSDN 原创文章 4获赞 0访问量 906 关注 私信
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: