如何将对象传递给构造函数? [英] How to pass objects to constructor?

查看:86
本文介绍了如何将对象传递给构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我是一名Java开发人员,现在我正在努力学习C ++。我是编程语言的新手。我正在尝试将对象传递给构造函数,我似乎做错了。


三角形对象需要包含对3Vertex的引用。对象,这是三角形的点。


Triangle.h

Hi,

I am a Java developper, and now I''m trying to learn C++. I''m very new to the programming language. I''m trying to pass objects to a constructor, which I appear to be doing wrong.

A triangle-object needs to contain references to 3 "Vertex" objects, which are the points of the triangle.

Triangle.h

展开 | 选择 | 换行 | 行号

推荐答案

它是说triangle.cpp的第2行
it is saying that line 2 of triangle.cpp
展开 | 选择 | Wrap | 行号


我知道,但我不明白为什么。我正在尝试做的是为三角形创建一个构造函数,它将三个Vertex对象作为参数。我真的没看到如何让顶点的默认构造函数相关。


我选择只提供一个带有x,y,z坐标的Vertex的构造函数。三角形的构造函数在这里没有任何关注,它应该只接受我使用它时给出的三个顶点对象,无论用于创建顶点对象的构造函数。


那么为什么不这样做呢?为什么我需要默认构造函数?我只想通过引用传递对象
I know, but I can''t see why. What I''m trying to do there is creating a constructor for a Triangle that takes three Vertex-objects as parameters. I really don''t see how having a default constructor for a vertex is relevant.

I choose to only provide a constructor for a Vertex that takes x,y,z - coordinates. The constructor for a Triangle has no concern in this, it should just accept whatever three vertex-objects I give it when I use it, regardless of the constructor used to create the vertex-objects.

So why doesn''t it just do that? Why do I need a default constructor? I just want to pass objects by reference


使用构造函数初始化列表:

Triangle :: Triangle(Vertex& one,Vertex& two,Vertex& amp;三){

:v1(一)

,v2(二)

{

//其余构造函数

..
use constructor initializers list:
Triangle::Triangle(Vertex & one, Vertex & two, Vertex & three) {
: v1(one)
, v2(two)
{
// the rest of constructor
..


这篇关于如何将对象传递给构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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