我看到了这个节目。这是什么 - 构造函数复数( double r, double i )之后的 re(r), im(i) {} ?

发布于 2024-12-08 17:57:08 字数 679 浏览 1 评论 0原文

// 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

今天小雨转甜 2024-12-15 17:57:08
Complex( double r, double i ) : re(r), im(i) {}

该构造函数在 C++ 中称为成员初始值设定项列表

初始化您的成员re为值r,并将成员im初始化为值i.


构造函数中的初始化和赋值有什么区别? &
有什么好处?

使用初始值设定项列表初始化成员与在构造函数体内为其赋值是有区别的。

当您通过初始值设定项列表初始化字段时,构造函数将被调用一次。

如果您使用赋值,那么字段将首先使用默认构造函数的数据进行初始化,然后使用实际值重新赋值(通过赋值运算符)。

正如您所看到的,创建和创建有额外的开销。后者的赋值对于用户定义的类来说可能相当重要。

对于双精度数据类型(您使用它)或 POD 类成员,没有实际开销。

Complex( double r, double i ) : re(r), im(i) {}

This constructor is called a Member Initializer List in C++.

It initializes your member re to a value r, and member im to a value i.


What is the difference between Initializing And Assignment inside a constructor? &
What is the advantage?

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.

小苏打饼 2024-12-15 17:57:08

这些是成员初始值设定项。当使用该构造函数创建对象时,成员将根据这些构造函数进行初始化。

因此,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 to r. And im gets set to i.

旧时模样 2024-12-15 17:57:08

这是一个初始化列表。它初始化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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文