构造函数中冒号后的变量 [英] Variables after the colon in a constructor

查看:19
本文介绍了构造函数中冒号后的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍在学习 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

这篇关于构造函数中冒号后的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆