English 中文(简体)
Doom 3 source:为什么代表2名飞行员的班子两次超载次稿操作员?
原标题:Doom 3 source: why does a class representing 2-vectors overload the subscript operator twice?

I thought I d try beefing up my C++ and OpenGL by looking at the recently-released Doom 3 source. Much learned so far, but I ve hit a wall. The class detailed here has methods

float  operator[] (int index) const  

and

float &  operator[] (int index) 

两个机构改为

return ( &x )[ index ];

<代码>x为2类数据成员之一(另一个为y;这一类别为2名查询人)。

虽然我可以理解每个版本的头盔/提纲,但我看不出他们为什么都在场。

const seems to appear (or not appear, as preferred) only to distinguish the headers sufficiently to allow compilation. (That is, remove const and VS2010 refuses to compile, similarly if both headers end in const.)

And why return a ref to a float? None of the class seven other float-type methods do this, so I m guessing efficiency isn t a factor (tho maybe this operator s called vastly more often than the others).

Appreciate any insight as to what s going on here...

最佳回答

这是一个共同的主轴(称为“最重载”)。 见C++ FAQ:。 http://www.paragia.com/c++-faq-lite/const-correctness.html#faq-18.12

模棱两可的解决方式是:*thisconst。 在<条码>const的物体上,const上载重,在这种情况下,该代码按读写方式行事。 关于非<条码>const的标语,不<条码>const,在这种情况下,它以读/正文的形式行事。

必须指出,这是而不是区分阅读和书写途径的一种方式。

问题回答

将它们作为附加说明的内容的相关奶粉。 <代码>float &营运人[](int index)为setter<>/em>版本,并允许你使用等同的合成物:

theObject[anIndex] = 1.0;

This requires that theObject is available to you as a non-const object (or through an Object * or Object &).

In the case without const, you are using a reference because you want to change the value when calling the function. Eg setting a value: a[11] = 5.0;

添加了“星号”的情形,因为其他职能只有在其要求的所有其他职能也一致的情况下才能宣布为“星号”职能。

为什么返回浮体?

The reason to return a reference to a float is to allow the caller to modify that float. (By the way, efficiency wouldn t be a reason for this, since pointers tend to be at least as big as floats, so the extra indirection is pure cost.)

const at the end of a means signature indicated that it can safe be calls on a const Object/ reference/expression. 因此,如果<代码>v is an idVec2,则v[0]可用作升值(和v[0] = 3.0f将实际设定v[0],但如果v is acode>const idVec2 ,则,则只能用作滚动值(http://meaning that





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?