我看到了这个节目。这是什么 - 构造函数复数( double r, double i )之后的 re(r), im(i) {} ?
// operator_overloading.cpp
// compile with: /EHsc
#include <iostream>
using namespace std;
struct Complex {
Complex( double r, double i ) : re(r), im(i) {} // what is this syntax?
Complex operator+( Complex &other );
void Display( ) { cout << re << ", " << im << endl; }
private:
double re, im;
};
// Operator overloaded using a member function
Complex Complex::operator+( Complex &other ) {
return Complex( re + other.re, im + other.im );
}
int main() {
Complex a = Complex( 1.2, 3.4 );
Complex b = Complex( 5.6, 7.8 );
Complex c = Complex( 0.0, 0.0 );
c = a + b;
c.Display();
}
// operator_overloading.cpp
// compile with: /EHsc
#include <iostream>
using namespace std;
struct Complex {
Complex( double r, double i ) : re(r), im(i) {} // what is this syntax?
Complex operator+( Complex &other );
void Display( ) { cout << re << ", " << im << endl; }
private:
double re, im;
};
// Operator overloaded using a member function
Complex Complex::operator+( Complex &other ) {
return Complex( re + other.re, im + other.im );
}
int main() {
Complex a = Complex( 1.2, 3.4 );
Complex b = Complex( 5.6, 7.8 );
Complex c = Complex( 0.0, 0.0 );
c = a + b;
c.Display();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
该构造函数在 C++ 中称为成员初始值设定项列表。
它初始化您的成员
re
为值r
,并将成员im
初始化为值i.
使用初始值设定项列表初始化成员与在构造函数体内为其赋值是有区别的。
当您通过初始值设定项列表初始化字段时,构造函数将被调用一次。
如果您使用赋值,那么字段将首先使用默认构造函数的数据进行初始化,然后使用实际值重新赋值(通过赋值运算符)。
正如您所看到的,创建和创建有额外的开销。后者的赋值对于用户定义的类来说可能相当重要。
对于双精度数据类型(您使用它)或 POD 类成员,没有实际开销。
This constructor is called a Member Initializer List in C++.
It initializes your member
re
to a valuer
, and memberim
to a valuei
.There is a difference between Initializing a member using initializer list and assigning a value to it inside the constructor body.
When you initialize fields via initializer list the constructors will be called once.
If you use the assignment then the fields will be firstly initialized with default constructor's data and then reassigned (via assignment operator) with actual values.
As you see there is an additional overhead of creation & assignment in the latter, which might be considerable for user defined classes.
For a double data type (for which you use it) or POD class members there is no practical overhead.
这些是成员初始值设定项。当使用该构造函数创建对象时,成员将根据这些构造函数进行初始化。
因此,
re
被设置为r
。并且im
设置为i
。Those are member initializers. When the object is created with that constructor, the members are initialized according to those.
So
re
gets set tor
. Andim
gets set toi
.这是一个初始化列表。它初始化
Complex
的成员。初始化列表可用于显式初始化类的成员。如果您不以这种方式初始化成员,它们将被默认初始化。某些类型的成员必须以这种方式初始化,例如引用(因为它们不能默认初始化)或没有默认构造函数的类。
此语法还可用于将参数传递给超类的构造函数。
This is a initializer-list. It initializes the members of
Complex
.An initializer-list can be used to explicitly initialize the members of a class. If you do not initialize your members in this way, they will get default-initialized. Some types of member have to be initialized in that way, for instance references (because they cannot be default initialized), or classes that have no default-constructor.
This syntax can also be used to pass parameters to the constructors of a superclass.