奇怪的参考声明? [英] Weird reference declaration?

查看:65
本文介绍了奇怪的参考声明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿人,


这看起来很奇怪。我无法理解它。这是一个错误吗?

任何人都可以帮忙吗?

IStack const& GetStack()const;

干杯,


Deets

解决方案

" ; Anon电子邮件 <一个******** @ fastmail.fm>在消息中写道

news:83 ************************* @ posting.google.co m

嘿人,

这看起来很奇怪。我无法理解它。这是一个错误吗?
任何人都可以帮忙吗?

IStack const& GetStack()const;

干杯,

Deets




这是一个名为GetStack的成员函数的声明。它返回一个对IStack对象的

const引用。最后的const表示调用

GetStack不会更改调用它的类对象。只有

成员函数声明const可以从const类对象中调用。

-

John Carson

1.要回复电子邮件地址,请删除donald

2.不要回复电子邮件地址(在此处发帖)


< blockquote>

" Anon Email" <一个******** @ fastmail.fm>在消息中写道

news:83 ************************* @ posting.google.co m ... < blockquote class =post_quotes>嘿人,

这看起来很奇怪。我无法理解它。这是一个错误吗?
任何人都可以帮忙吗?

IStack const& GetStack()const;




这与

const IStack& GetStack()const


如果有帮助 - 我发现这个组中的当前趋势是将const

放在类型之后最多

令人困惑和不受欢迎,因为它与我曾经遇到的所有历史C使用相反。


如果你不理解我的话重写然后你需要先读一本C ++书

因为它是

真正基本的东西。


我稍微有用的例子可能是


IStack const * stack;

const IStack * stack;

IStack * const stack;


前两个定义一个指向常量IStack的可变指针,但

最后一个

定义一个指向可变IStack的常量指针。 />
作为一种方法的等价物并没有多大作用,因为返回一个常量

指针

对任何事情都没有影响(模板功能问题除外) 。


返回一个常量指针


" Nick Hounsome" < NH *** @ blueyonder.co.uk>在消息中写道

新闻:8W ************ @ news-binary.blueyonder.co.uk ...


[snip]

我稍微有用的例子可能是

IStack const * stack;
const IStack * stack;
IStack * const堆栈;

前两个定义一个指向常量IStack的可变指针但
最后一个定义了一个指向可变IStack的常量指针。




而且最后一个语法的结果是导致一些人使用第一个语句的原因:为了一致性而使用。 />


Hey people,

This looks really weird. I can''t make sense of it. Is this a mistake?
Can anyone help?
IStack const & GetStack () const;
Cheers,

Deets

解决方案

"Anon Email" <an********@fastmail.fm> wrote in message
news:83*************************@posting.google.co m

Hey people,

This looks really weird. I can''t make sense of it. Is this a mistake?
Can anyone help?
IStack const & GetStack () const;
Cheers,

Deets



This is the declaration of a member function called GetStack. It returns a
const reference to an IStack object. The final const means that calling
GetStack does not change the class object from which it is called. Only
member functions declared const can be called from a const class object.
--
John Carson
1. To reply to email address, remove donald
2. Don''t reply to email address (post here instead)



"Anon Email" <an********@fastmail.fm> wrote in message
news:83*************************@posting.google.co m...

Hey people,

This looks really weird. I can''t make sense of it. Is this a mistake?
Can anyone help?
IStack const & GetStack () const;



This is exactly the same as
const IStack& GetStack() const

If that helps - I find the current trend in this group to put the const
after the type as most
confusing and unwelcome since it is counter to all historical C usage that I
have ever come across.

If you don''t understand my rewrite then you need to read a C++ book first
because it is
really basic stuff.

I slightly more useful example might be

IStack const* stack;
const IStack* stack;
IStack* const stack;

Where the first two define a changeable pointer to a constant IStack but the
last one
defines a constant pointer to an changeable IStack.
The equivalent as a method doesn''t acheive much because returning a constant
pointer
has no effect on anything (other than template functuion issues).

Which returns a constant pointer


"Nick Hounsome" <nh***@blueyonder.co.uk> wrote in message
news:8W************@news-binary.blueyonder.co.uk...

[snip]

I slightly more useful example might be

IStack const* stack;
const IStack* stack;
IStack* const stack;

Where the first two define a changeable pointer to a constant IStack but the last one
defines a constant pointer to an changeable IStack.



And imo the syntax of that last one is what causes some folks
to use the first one: in the interest of ''consistency''.


这篇关于奇怪的参考声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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