- > vs。 [英] -> vs .

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

问题描述

我想知道当使用时是否有任何重大差异(速度/大小) - >

vs.返回一个指针vs一个引用。


i..e在我的代码中我重载[]很多(因为我有很多类)我可以做




object& operator [](int index)


{


if((index> = NumProperties())||(index< 0 )||(Properties.empty()))


{


throw" Object:Invalid Index!" ;;


}


返回*(属性[索引]);


};
< br $>



object * operator [](int index)


{


if((index> = NumProperties())||(index< 0)||(Properties.empty()))


{


throw"对象:索引无效!" ;;


}


return(属性[index] );


};


属性是一个指针向量。这两种方法的区别

basicaly归结为使用 - >和。但我更喜欢使用。因为它更容易打字并且与我对字段的思考一致(而且>是基本相同的,因为它更容易思考。(不多但是。) ..))。


我只是想知道是否有任何理由不在第二个代码示例中使用第一个代码示例

(目前为止)关于优化)。


谢谢,


Jon

解决方案



Jon Slaughter写道:

我想知道当使用时是否有任何重大差异(速度/大小) - >
vs。返回指针与引用。




速度/大小没有区别。


< blockquote>

" Maxim Yegorushkin" <毫安*************** @ gmail.com>在消息中写道

news:11 ********************* @ g14g2000cwa.googlegro ups.com ...


Jon Slaughter写道:

我想知道当一个
使用时是否有任何重大差异(速度/大小) - >
vs。返回一个指针vs一个引用。



速度/大小没有区别。




好​​,这就是我的意思想但是不确定。


谢谢,


Jon

" Jon Slaughter" <乔*********** @ Hotmail.com>在消息中写道

新闻:11 ************* @ corp.supernews.com ...

我想知道是否有当
使用时,任何大的差异(速度/大小) - > vs。返回一个指针vs一个引用。

我在我的代码中我重载了很多[因为我有很多类)我可以

<对象& operator [](int index)

if((index> = NumProperties())||(index< 0)||(Properties.empty( )))



抛出对象:索引无效!;

}
返回* (属性[index]);

};


对象* operator [](int index)

> {

if((index> = NumProperties())||(index< 0)||(Properties.empty()))

{

抛出对象:索引无效!;

}
返回(属性[索引]);

};

属性是指针的向量。这两种方法的区别基本上归结为使用 - >和。但我更喜欢使用。因为它更容易打字并且与我对字段的思考一致(而 - >
基本相同,它更容易思考。(不多但是......))。

我只是想知道是否有任何理由不使用第一个代码
示例而不是第二个(就优化而言)。




速度/尺寸可能没什么区别,但是你可以测试一下

肯定。


就我个人而言如果

返回值可能为null,则仅使用指针版本。传统上,operator []按值返回参考或




-

David Hilsee


I was wondering if there is any big difference(speed/size) when one uses ->
vs . as returning a pointer vs a reference.

i..e in my code I overload [] a lot(cause I have many classes) and I can do
either

object& operator[](int index)

{

if ((index >= NumProperties()) || (index < 0) || (Properties.empty()))

{

throw "Object: Invalid Index!";

}

return *(Properties[index]);

};

or

object* operator[](int index)

{

if ((index >= NumProperties()) || (index < 0) || (Properties.empty()))

{

throw "Object: Invalid Index!";

}

return (Properties[index]);

};

Properties is a vector of pointers. The difference between the two methods
basicaly comes down to using -> and . but I prefer to use . because its
easier to type and consistent with my thinking about fields(while the -> is
basicaly the same its just easier to think about .(not much but...)).

I''m just wondering if there is any reason not to use the first code example
over the second one(as far as optimization is concerned).

Thanks,

Jon

解决方案


Jon Slaughter wrote:

I was wondering if there is any big difference(speed/size) when one uses ->
vs . as returning a pointer vs a reference.



There is no difference in speed/size.



"Maxim Yegorushkin" <ma***************@gmail.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...


Jon Slaughter wrote:

I was wondering if there is any big difference(speed/size) when one
uses ->
vs . as returning a pointer vs a reference.



There is no difference in speed/size.



good, thats what I thought but wasn''t sure.

Thanks,

Jon


"Jon Slaughter" <Jo***********@Hotmail.com> wrote in message
news:11*************@corp.supernews.com...

I was wondering if there is any big difference(speed/size) when one uses -> vs . as returning a pointer vs a reference.

i..e in my code I overload [] a lot(cause I have many classes) and I can do either

object& operator[](int index)

{

if ((index >= NumProperties()) || (index < 0) || (Properties.empty()))

{

throw "Object: Invalid Index!";

}

return *(Properties[index]);

};

or

object* operator[](int index)

{

if ((index >= NumProperties()) || (index < 0) || (Properties.empty()))

{

throw "Object: Invalid Index!";

}

return (Properties[index]);

};

Properties is a vector of pointers. The difference between the two methods
basicaly comes down to using -> and . but I prefer to use . because its
easier to type and consistent with my thinking about fields(while the -> is basicaly the same its just easier to think about .(not much but...)).

I''m just wondering if there is any reason not to use the first code example over the second one(as far as optimization is concerned).



There probably isn''t a difference in speed/size, but you can test to be
sure.

Personally, I''d only use the pointer version if it were possible for the
return value to be null. Traditionally, operator[] returns a reference or
by value.

--
David Hilsee


这篇关于 - &GT; vs。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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