Variables after the colon in a constructor(构造函数中冒号后的变量)
问题描述
我仍在学习 C++ 并试图理解它.我正在查看一些代码并看到:
I am still learning C++ and trying to understand it. I was looking through some code and saw:
point3(float X, float Y, float Z) :
x(X), y(Y), z(Z) // <----- what is this used for
{
}
构造函数参数旁边的x(X), y(Y), z(Z)"是什么意思?
What is the meaning of the "x(X), y(Y), z(Z)" sitting beside the constructor's parameters?
推荐答案
这是一种调用 point3 类成员的构造函数的方法.如果 x、y 和 z 是浮点数,那么这只是一种更有效的写法
It's a way of invoking the constructors of members of the point3 class. if x,y, and z are floats, then this is just a more efficient way of writing this
point3( float X, float Y, float Z):
{
x = X;
y = Y;
z = Z;
}
但是如果 x, y &z 是类,那么这是将参数传递给它们的构造函数的唯一方法
But if x, y & z are classes, then this is the only way to pass parameters into their constructors
这篇关于构造函数中冒号后的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:构造函数中冒号后的变量


- 静态初始化顺序失败 2022-01-01
- 与 int by int 相比,为什么执行 float by float 矩阵乘法更快? 2021-01-01
- 一起使用 MPI 和 OpenCV 时出现分段错误 2022-01-01
- 使用/clr 时出现 LNK2022 错误 2022-01-01
- 如何对自定义类的向量使用std::find()? 2022-11-07
- Stroustrup 的 Simple_window.h 2022-01-01
- STL 中有 dereference_iterator 吗? 2022-01-01
- C++ 协变模板 2021-01-01
- 从python回调到c++的选项 2022-11-16
- 近似搜索的工作原理 2021-01-01