C++ 将参数传递给 class 的实例
C++ passing parameters to an instance of a class
我正在调试一些用 C++ 编写的模拟软件,并且无法理解部分代码。
该软件模拟了一些在 3D 世界中移动的实体,它们的行为可以设置为从 A 到 B 沿直线移动、绕特定点旋转或遵循预定义的路线等。
当实体在模拟中移动时,有关其移动的一些信息会呈现给用户 - 例如:其当前速度、航向(方向)、到当前目标的距离、到达所需时间它当前的目标和该位置的预计到达时间等
我目前正在处理的错误是旅程的预计到达时间和 'time to finish current leg' 向用户显示的值不正确。
我在向用户显示此信息的 C++ 源文件中遇到了以下代码:
DateTimeAndZone fpETA(mCurrentTime);
SystemTimeTypes::addRelativeTimeToSystemTime(fpETA, flightPlanETA);
DateTimeAndZone
是一个 class,所以 fpETA
是 class 的一个实例...我不明白的是 mCurrentTime
(这也是一个 DateTimeAndZone
),可以作为一个 class 实例的参数传递 - 看起来与将参数传递给函数的方式相同。
如果我转到 DateTimeAndZone
class 的定义,在它的末尾有一个 return
语句 returns a static std::string_classname
.
任何人都可以向我解释如何将 class 作为参数传递以创建新的 class 吗?这意味着什么?这是否意味着新 class 将采用现有 class 中存储的信息?或者我在这里遗漏了什么?
我在 SO 上遇到了这个问题,但它并没有真正回答我的问题...Passing a class object as an argument in C++
DateTimeAndZone fpETA(mCurrentTime);
调用DateTimeAndZone的构造函数,定义参数。如果 mCurrentTime 是一个 DateTimeAndZone 对象,默认会调用复制构造函数,如果已经定义,则调用 DateTimeAndZone 的复制构造函数。
这称为复制构造函数。它通过从 mCurrentTime
对象复制数据来创建一个新的 DateTimeAndZone
对象。这种函数的签名通常是:
DateTimeAndZone (const &DateTimeAndZone);
如果您在 class 中查看具有该签名的方法的定义,您应该会看到这个特定的复制构造函数的作用。
A class 可以像任何其他类型一样作为参数传递,您确实需要查看该构造函数的实现(如果没有文档)以了解它使用参数的目的.
通常使用这种类型的构造函数称为复制构造函数,有很多问题解释了复制构造函数的使用以及它们带来的含义:
When do we have to use copy constructors?
The copy constructor and assignment operator
What is a copy constructor in C++?
What is the difference between a deep copy and a shallow copy?
以下:
class DateTimeAndZone;
DateTimeAndZone fpETA(mCurrentTime);
// ^ this invokes:
DateTimeAndZone::DateTimeAndZone(const DateTimeAndZone &arg);
最后一行是DateTimeAndZone
class的拷贝构造函数。此复制构造函数将创建一个新的 DateTimeAndZone
对象,该对象将保存您传递的参数的副本(在这种情况下 mCurrentTime
)。
复制构造函数可以由编译器自动生成(你可以研究this slideshare看看在什么情况下是或不是)或者用户定义。
我正在调试一些用 C++ 编写的模拟软件,并且无法理解部分代码。
该软件模拟了一些在 3D 世界中移动的实体,它们的行为可以设置为从 A 到 B 沿直线移动、绕特定点旋转或遵循预定义的路线等。
当实体在模拟中移动时,有关其移动的一些信息会呈现给用户 - 例如:其当前速度、航向(方向)、到当前目标的距离、到达所需时间它当前的目标和该位置的预计到达时间等
我目前正在处理的错误是旅程的预计到达时间和 'time to finish current leg' 向用户显示的值不正确。
我在向用户显示此信息的 C++ 源文件中遇到了以下代码:
DateTimeAndZone fpETA(mCurrentTime);
SystemTimeTypes::addRelativeTimeToSystemTime(fpETA, flightPlanETA);
DateTimeAndZone
是一个 class,所以 fpETA
是 class 的一个实例...我不明白的是 mCurrentTime
(这也是一个 DateTimeAndZone
),可以作为一个 class 实例的参数传递 - 看起来与将参数传递给函数的方式相同。
如果我转到 DateTimeAndZone
class 的定义,在它的末尾有一个 return
语句 returns a static std::string_classname
.
任何人都可以向我解释如何将 class 作为参数传递以创建新的 class 吗?这意味着什么?这是否意味着新 class 将采用现有 class 中存储的信息?或者我在这里遗漏了什么?
我在 SO 上遇到了这个问题,但它并没有真正回答我的问题...Passing a class object as an argument in C++
DateTimeAndZone fpETA(mCurrentTime);
调用DateTimeAndZone的构造函数,定义参数。如果 mCurrentTime 是一个 DateTimeAndZone 对象,默认会调用复制构造函数,如果已经定义,则调用 DateTimeAndZone 的复制构造函数。
这称为复制构造函数。它通过从 mCurrentTime
对象复制数据来创建一个新的 DateTimeAndZone
对象。这种函数的签名通常是:
DateTimeAndZone (const &DateTimeAndZone);
如果您在 class 中查看具有该签名的方法的定义,您应该会看到这个特定的复制构造函数的作用。
A class 可以像任何其他类型一样作为参数传递,您确实需要查看该构造函数的实现(如果没有文档)以了解它使用参数的目的.
通常使用这种类型的构造函数称为复制构造函数,有很多问题解释了复制构造函数的使用以及它们带来的含义:
When do we have to use copy constructors?
The copy constructor and assignment operator
What is a copy constructor in C++?
What is the difference between a deep copy and a shallow copy?
以下:
class DateTimeAndZone;
DateTimeAndZone fpETA(mCurrentTime);
// ^ this invokes:
DateTimeAndZone::DateTimeAndZone(const DateTimeAndZone &arg);
最后一行是DateTimeAndZone
class的拷贝构造函数。此复制构造函数将创建一个新的 DateTimeAndZone
对象,该对象将保存您传递的参数的副本(在这种情况下 mCurrentTime
)。
复制构造函数可以由编译器自动生成(你可以研究this slideshare看看在什么情况下是或不是)或者用户定义。