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
这篇关于构造函数中冒号后的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:构造函数中冒号后的变量


基础教程推荐
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 常量变量在标题中不起作用 2021-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 这个宏可以转换成函数吗? 2022-01-01