简体   繁体   English

一元运算符重载&

[英]Overloading unary operator &

Let's consider a class with overloaded unary operator & (Address-of). 让我们考虑一元运算符&(Address-of)重载的类。 Let it be class A 让它成为class A

template <class C>
class A
{
public:
    C * operator &()
    {
        return &data;
    }
    //...
private:
    C data;
}

Now I want to pass to some function a pointer of type A to fill its data . 现在,我想向某个函数传递类型为A的指针以填充其data Let us call it f 让我们称它为f

void f(A * auto_containter)
{
    //...
}

But it is clear why the code bellow wouldn't work (even wouldn't compile). 但是很明显,为什么下面的代码行不通(甚至无法编译)。 It is because the overloaded operator is called. 这是因为调用了重载运算符。

A a;
f(&a);

The question is following: 问题如下:

Is there any syntax to pass address of a to f ? 是否有任何语法的地址传递af If no, then for me it is very strange why it is allowed to overload unary operator & , because it makes code more buggy and difficult to understand. 如果否,那么对我来说,为什么允许它重载一元运算operator &很奇怪,因为它使代码更容易出错且难以理解。 Or there are some other reasons? 还是有其他原因?

Is there any syntax to pass address of a to f ? 是否有任何语法的地址传递af

Yes, there's ugly syntax: 是的,语法很丑陋:

f( reinterpret_cast<A*>( &reinterpret_cast<char&>(a) ) );

boost::addressof is a nice and generic wrapper around it. boost::addressof是一个不错的通用包装器。

Use boost::addressof function. 使用boost::addressof函数。 In any case, overloading unary & is highly dubious. 无论如何,一元和超载是高度可疑的。 Why do it instead of having a named function that returns the address of your data? 为什么不使用命名函数返回数据地址呢?

Is there any syntax to pass address of a to f? 有什么语法可以将a的地址传递给f?

Others have already pointed out boost::addressof . 其他人已经指出boost::addressof The mechanism it relies on is a standard-guaranteed use of the built-in address operator for a reinterpret_cast to reference type. 它所依赖的机制是内置地址运算符的标准保证用法,用于将reinterpret_cast为引用类型。 The Boost function just wraps the rather verbose and awkward combination of casts. Boost函数只是包装了相当冗长而笨拙的转换组合。

If no, then for me it is very strange why it is allowed to overload unary operator &, because if will make code more buggy and difficult to understand. 如果否,那么对我来说,为什么允许重载一元运算符&很奇怪,因为如果这样做会使代码更加错误且难以理解。 Or there are some other reasons? 还是有其他原因?

In some cases it can be more convenient. 在某些情况下,它可能更方便。 For example, a smart pointer class might offer a custom address operator in order to support writing &p as actual argument to a T** formal argument. 例如,智能指针类可能会提供一个自定义地址运算符 ,以支持将&p作为实际参数写入T**形式参数。 However, I think nowadays it's generally recognized that it isn't all that good an idea. 但是,我认为,如今,人们普遍认为这并不是一个好主意。

Cheers & hth., 干杯,……

Why would you ever want to overload the unary operator& ? 为什么您要重载一元运算operator&

Aside from that, there is boost::addressof . 除此之外,还有boost::addressof

这就是为什么发明了boost::addressof原因。

Your scenario never really comes up, because anyone who is writing that function will take a reference, not a pointer. 您的情况永远不会真正出现,因为编写该函数的任何人都将引用而不是指针。 Plus, you forgot to instantiate A with an example type for C. 另外,您忘了使用C的示例类型实例化A。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM