简体   繁体   English

有助于理解构造函数,运算符重载,析构函数等

[英]A bit of help understanding constructors, operator overloads, destructors, etc

so I'm a C++ beginner and I have a question. 所以我是C ++初学者,我有一个问题。

Let's say we have a class Grades. 假设我们有一个班级成绩。

From what I've learned so far a destructor would look like 根据我到目前为止所学到的,析构函数看起来像

~Grades();

a copy constructor: 复制构造函数:

Grades(const Grades & );

an << operator: <<操作符:

ostream & operator << (ostream & os, const Grades & g);

Are these correct? 这些正确吗?

How would a regular constructor look like? 常规构造函数的外观如何? What about a conversion constructor ? 转换构造函数呢?

Regular default constructor, provided by compiler, will look like below and it will internally call the base class constructors if it is derived from any class. 编译器提供的常规默认构造函数如下所示,如果它是从任何类派生的,它将在内部调用base class构造函数。 And later for data members having user defined type, it will call their respective default constructors in the order of their declaration. 然后,对于具有用户定义类型的数据成员,它将按其声明顺序调用其各自的默认构造函数。

Grades();

The conversion constructors are something you will have to define, and they will look like 转换构造函数是您必须定义的东西,它们看起来像

Grade(const T&)

If you want conversion function, 如果要转换功能,

 Grade operator=(const T&)

The only destructor you could have is suppose to de-initialize the object by calling respective destructors if user defined data members and then for base classes, exactly in reverse order what default compiler provided constructor would provide. 您可能拥有的唯一destructor是,如果用户定义了数据成员,则通过调用相应的destructors函数对对象进行反初始化,然后针对基类,以与默认编译器提供的构造函数所提供的完全相反的顺序进行调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM