我是否通过构造函数中的值或引用传递int? [英] Do I pass an int by value or reference in constructor?

查看:67
本文介绍了我是否通过构造函数中的值或引用传递int?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的类来存储二维坐标:


class Point

{

public:

Point();

Point(const Point&);

Point& operator(const Point&);

int x;

int y;

};


如果我想要一个需要两个整数的构造函数,最好通过

值或参考?换句话说,Point(int,int)或Point(const

int&,const int&)? (或者也许是Point(const int,const int)?那个

实际上是否提高了性能?)

I have a very simple class for storing two-dimensional coordinates:

class Point
{
public:
Point();
Point(const Point &);
Point &operator(const Point &);
int x;
int y;
};

If I want a constructor that takes two ints, is it better to pass by
value or by reference? In other words, Point(int, int) or Point(const
int &, const int &)? (Or maybe Point(const int, const int)? Does that
actually improve performance?)

推荐答案

Point& ;操作员(const Point&);


哎呀,对不起,我在那里错过了一个角色。它应该是Point& operator

=(const Point&)。
Point &operator(const Point &);

Oops, sorry, I missed a character there. It should be Point &operator
=(const Point &).


7月3日,10:47 * pm,ampheta。 .. @ gmail.com写道:
On Jul 3, 10:47*pm, ampheta...@gmail.com wrote:

我有一个非常简单的类用于存储二维坐标:


class Point

{

public:

* * Point();

* * Point(const Point& );

* * Point& operator(const Point&);

* * int x;

* * int y;


};


如果我想要一个需要两个整数的构造函数,最好通过

值或通过参考?换句话说,Point(int,int)或Point(const

int&,const int&)? (或者可能是Point(const int,const int)?这是否实际上提高了性能?
I have a very simple class for storing two-dimensional coordinates:

class Point
{
public:
* * Point();
* * Point(const Point &);
* * Point &operator(const Point &);
* * int x;
* * int y;

};

If I want a constructor that takes two ints, is it better to pass by
value or by reference? In other words, Point(int, int) or Point(const
int &, const int &)? (Or maybe Point(const int, const int)? Does that
actually improve performance?)



对于内置类型,你不应该看到任何性能差异

通过引用或值传递。事实上,对于像你发送的一样简单的类,函数调用本身应该完全由

编译器优化。

Joe Cook

For built-in types, you shouldn''t see any performance difference
passing by reference or value. In fact, for a class as simple as the
one you sent, the function call itself should be optimized away by the
compiler entirely.

Joe Cook


7月3日晚上10点58分,joseph cook< joec ... @ gmail.comwrote:
On Jul 3, 10:58 pm, joseph cook <joec...@gmail.comwrote:

对于内置类型,你不应该看到任何性能差异

通过引用或值传递。事实上,对于像你发送的一样简单的类,函数调用本身应该完全由

编译器优化。

Joe Cook
For built-in types, you shouldn''t see any performance difference
passing by reference or value. In fact, for a class as simple as the
one you sent, the function call itself should be optimized away by the
compiler entirely.

Joe Cook



谢谢。但是,是否有关于是否通过

值或参考的指导方针?是一种或另一种是更好的风格?

Thanks. But still, are there any guidelines on whether to pass by
value or by reference? Is one or the other a "better style"?


这篇关于我是否通过构造函数中的值或引用传递int?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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