[英]Understanding the & operator in templates
so im currently reading sams Teach yourself C++ and am unable to figure out when and why the & operator is used, as it seems like it would be uneccesary in many cases like so: 所以我目前正在阅读sams教你自己C ++ ,我无法弄清楚何时以及为何使用&运算符,因为在许多情况下它似乎是不可取的,如下所示:
template <typename objectType>
objectType & GetMax (const objectType & value1, const objectType & value2)
{
if (value1 > value2)
return value1;
else
return value2;
};
Another example: 另一个例子:
Template <typename T>
class myTemplateClass
{
public:
void SetVariable (T& newValue) {m_Value = newValue; };
T& GetValue() {return m_Value;};
private:
T m_Value;
};
Please help me understand why &, which I know gets the address of the data type, is here! 请帮助我理解为什么&,我知道获取数据类型的地址,就在这里! its definately not making learning the STL any easier.......... THANKS!
它绝对没有让学习STL变得更容易..........感谢! =)
=)
That's not an operator, and it's not (directly) related to templates. 那不是运营商,也不是(直接)与模板相关。
It's a type modifier, creating (or indicating or denoting or forming) a reference, just like *
creates a pointer. 它是一个类型修饰符,创建(或指示或表示或形成)引用,就像
*
创建指针一样。
Much of the time, it's an optimization rather than a necessity. 大多数时候,这是一种优化而非必需品。
Two
Three cases where it is necessary:
两个
有必要的三个案例:
In the copy-constructor, it's needed to avoid infinite recursion: 在copy-constructor中,需要避免无限递归:
class X { //X(X); // pass by value, the formal parameter is a copy of the actual parameter // the copy constructor must be called to make the copy X(const X&); // pass by reference, ok, no copy constructor call necessary };
Const references in general save having to copy a large object, which is a useful optimization with no surprising behavior. 一般来说,Const引用不必复制大对象,这是一种有用的优化,没有任何令人惊讶的行为。
When the return type of a function, especially operator[]
overload, must appear on the left hand side of an expression: 当函数的返回类型(尤其是
operator[]
重载)必须出现在表达式的左侧时:
class Container { //Element operator[](int index); // problematic because c[i] is a temporary // so c[i] = 5; doesn't actually modify the collection, like it would with an array Element& operator[](int index); // correct
}; };
As a similar case, for operators that mutate their left operand, like compound assignment and stream insertion, it's necessary to use a non-const reference parameter. 作为类似的情况,对于改变左操作数的运算符,如复合赋值和流插入,必须使用非const引用参数。
Any other cases (eg output arguments) can (and I think should) be handled with pointers, since a function call that LOOKS like pass-by-value but changes its argument violates the principle of least-surprise. 任何其他情况(例如输出参数)都可以(并且我认为应该)使用指针来处理,因为LOOKS类似于值传递但更改其参数的函数调用违反了最小惊喜的原则。 Perfect example:
auto_ptr<int> a = new int(5); f(a); /* is a still valid !?! */
完美的例子:
auto_ptr<int> a = new int(5); f(a); /* is a still valid !?! */
auto_ptr<int> a = new int(5); f(a); /* is a still valid !?! */
Which brings us to case 3, (created because somebody used non-const references outside of operators): 这将我们带到案例3,(因为有人在运算符之外使用了非const引用而创建):
In templates, when the actual type might by auto_ptr
or unique_ptr
, a reference is needed to avoid inferring a type that destroys the original: 在模板中,当实际类型可能是
auto_ptr
或unique_ptr
,需要引用以避免推断破坏原始类型的类型:
auto_ptr<int> a = new int(5); //template<typename T> //void dump_target( T handle ) { cout << *item; } // bad: dump_target(a); destroys *a template<typename T> void dump_target( const T& handle ) { cout << *item; } // correct
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.