const ref与指针 [英] const ref vs. pointer

查看:67
本文介绍了const ref与指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个问题:


1.哪一个是最好的:通过const返回类的成员变量

ref还是通过指针返回?


例如:


员工* GetEmployee()const;

员工& GetEmployee()const;


2.哪一个最好:通过const ref或指针传递变量?


例如:


void SetSalary(Employee * e);

void SetSalary(const Employee& e);


谢谢

I have 2 questions:

1. Which one is the best: Returning member variables of the class by const
ref or returning by pointer?

e.g.:

Employee* GetEmployee() const;
Employee& GetEmployee() const;

2. Which one is best: Passing a variable by const ref or by pointer?

e.g.:

void SetSalary(Employee* e);
void SetSalary(const Employee& e);

Thanks

推荐答案

这些中没有明确的最佳选择。答案是

取决于你的函数的行为方式。


参考和指针之间最重要的区别是

指针可以为null但是引用总是指向一些有效的对象。


所以如果GetEmployee有时必须返回null Employee使用

指针其他明智的使用引用。该参考将有所帮助因为你

将始终确保该引用指向一些有效的Employee。


引用是alwyas常量,你不能更改引用

ponit到其他一些对象。所以你可以取消const

说明符。正如我之前所说,如果对象始终有效,

引用总是比指针好。


谢谢

ravinder thakur


Luca写道:
there is no clear cut best option among these. the answer being
dependent upon the way your function behaves.

the most important difference between the reference and pointer is that
pointer can be null but reference always points to some valid object.

So if GetEmployee will sometime have to return null Employee use
pointer other wise use reference. The reference will help because you
will always be sure that the reference points to some valid Employee.

Reference are alwyas constants, you cannot change the reference to
ponit to some other object. So you can do away with the const
specifier. And as i said before, if the objects are always valid,
reference are always better than pointers.

thanks
ravinder thakur

Luca wrote:

我有2个问题:


1哪一个是最好的:通过const返回该类的成员变量

ref或通过指针返回?


例如:

员工* GetEmployee()const;

员工& GetEmployee()const;


2.哪一个最好:通过const ref或指针传递变量?


例如:


void SetSalary(Employee * e);

void SetSalary(const Employee& e);


谢谢
I have 2 questions:

1. Which one is the best: Returning member variables of the class by const
ref or returning by pointer?

e.g.:

Employee* GetEmployee() const;
Employee& GetEmployee() const;

2. Which one is best: Passing a variable by const ref or by pointer?

e.g.:

void SetSalary(Employee* e);
void SetSalary(const Employee& e);

Thanks




Luca写道:

Luca wrote:

我有2个问题:


1.哪一个是最好的:通过const返回类的成员变量

ref还是通过指针返回?


例如:


员工* GetEmployee()const;

员工& GetEmployee()const;
I have 2 questions:

1. Which one is the best: Returning member variables of the class by const
ref or returning by pointer?

e.g.:

Employee* GetEmployee() const;
Employee& GetEmployee() const;



员工const&

Employee const&


>

2.哪一个最好:通过const ref或指针传递变量?


例如:


void SetSalary(员工* e);

void SetSalary(const员工& e);
>
2. Which one is best: Passing a variable by const ref or by pointer?

e.g.:

void SetSalary(Employee* e);
void SetSalary(const Employee& e);



在这两种情况下(如在生活的所有其他方面;-)选择最适合您目的的构造

。在这里,你应该明白你应该使用const& ;.
。总会有一个值返回,必须

始终是一个值。使用指针发送不同的消息

并邀请例如叫SetSalary(0)。


/ Peter

In both cases (as in all other aspects of life ;-) choose the construct
that suits your purpose best. Here, it seems evident that you should
use a const&. There will always be a value to return and there must
always be a value to supply. Using a pointer sends a different message
and invites to e.g. call SetSalary(0).

/Peter


Luca写道:
Luca wrote:

我有两个问题:


1.哪一个是最好的:返回const类的成员变量

ref或者通过指针返回?


例如:


员工* GetEmployee()const;

员工& GetEmployee()const;
I have 2 questions:

1. Which one is the best: Returning member variables of the class by const
ref or returning by pointer?

e.g.:

Employee* GetEmployee() const;
Employee& GetEmployee() const;



如果上面的内容返回成员变量,那么他们应该返回指针或

对const的引用。


const Employee * GetEmployee()const;

const Employee& GetEmployee()const;


两者都不比另一个更好,虽然后者对于来电者来说可能是更自然的b / b $



cout<< *(x.GetEmployee()); //使用指针返回


vs


cout<< x.GetEmployee(); //使用返回的引用


但它在很大程度上取决于您的编码风格和上下文。

If the aboves return member variables then they should return pointer or
reference to const.

const Employee* GetEmployee() const;
const Employee& GetEmployee() const;

Neither is better than the other although the latter might be more
natural for the caller:

cout << *(x.GetEmployee()); // using a pointer returned

vs

cout << x.GetEmployee(); // using a reference returned

But it highly depends on your coding style and context.


>

2.哪一个最好:通过const ref或指针传递变量?


例如:


void SetSalary (员工* e);

void SetSalary(const Employee& e);
>
2. Which one is best: Passing a variable by const ref or by pointer?

e.g.:

void SetSalary(Employee* e);
void SetSalary(const Employee& e);



不应将指针与const的引用进行比较。你要么是
比较一个指针和一个参考


//如果要改变参数

void SetSalary(Employee * e);

无效SetSalary(员工& e);


或指向const的点,带有const


//如果不改变参数

void SetSalary(const Employee * e);

void SetSalary(const Employee& e);


无论你有哪种情况,你都可以选择使用

指针或参考。

A pointer should not be compared to a reference to const. You either
compare a pointer with a reference

// if the argument is to be changed
void SetSalary(Employee* e);
void SetSalary(Employee& e);

or a point to const with a reference to const

// if the argument is not to be changed
void SetSalary(const Employee* e);
void SetSalary(const Employee& e);

Whichever case you have, it is up to you whether you choose to use
pointer or reference.


>

谢谢
>
Thanks



Ben

Ben


这篇关于const ref与指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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