简体   繁体   English

为什么Glibmm / Gtkmm不包括用于Gl​​ib :: RefPtr的一元解引用运算符*?

[英]Why does Glibmm/Gtkmm not include the unary dereferencing operator, *, for Glib::RefPtr?

Glib::RefPtr allows dereferencing via ' -> ' but not via ' * '. Glib :: RefPtr允许通过' -> '解除引用,但不能通过' * '解除引用。 Why is this? 为什么是这样?

I can of course do: 我当然可以这样做:

 class Foo {};
 Glib::RefPtr<Foo> fooPtr;

 fooPtr.operator->();

The docs specifically mention that they left operator*() out. 文档特别提到他们离开了operator *()。 But they do not offer any guidance as to why. 但他们没有提供任何指导原因。

Edited with example for clarity : 为清晰起见,编写了示例

I've seen it argued that " you should never need to dereference " a RefPtr, but IMO that seems bogus counterintuitive as any function that wants to be used with both dynamically and stack allocated objects would need the lowest common denominator interface, ie pass-by-reference. 我已经看到了它认为:“ 你不应该需要取消引用 ”一个RefPtr,但IMO似乎违反直觉 作为希望与这两个动态和堆栈分配对象需要的最小公分母接口,即pass-可以使用任何功能引用。

Take, for instance the following example: 举个例子,例如:

struct Foo 
{ 
    void print() { printf( "Success" ); } 
};

void myFunc( const Foo & foo ) { foo.print(); }

int main()
{
    Foo               foo0;
    Glib::RefPtr<Foo> foo1Ptr( new Foo );

    myFunc(  foo0    );
    myFunc( *foo1Ptr ); // error, no operator*()

    return 0;
}

Anyone know why this position is taken by the Glib team? 任何人都知道为什么Glib团队会采取这个立场?

Some functions may take objects, or object references as args. 某些函数可能将对象或对象引用作为args。

No, if an object should be used via a RefPtr, then no function will (or no function should) take it without RefPtr. 不,如果一个对象应该通过RefPtr使用,那么没有RefPtr就没有函数(或者没有函数应该)。 Not having operator* avoids people doing this, meaning that APIs are forced to be correct, meaning that there are less memory management errors due to not using RefPtr. 没有运算符*可以避免人们这样做,这意味着API被强制正确,这意味着由于不使用RefPtr而导致内存管理错误减少。 It's not there because people would misuse it, and people have a hard enough time getting the hang of smartpointers already. 它不在那里因为人们会滥用它,人们已经有足够的时间来掌握智能指针。

If you have a real problem that you think would be solved by a RefPtr::operator*(), then you might mention the actual problem so we can (probably) show how you don't really want to use operator*() there. 如果你有一个你认为可以通过RefPtr :: operator *()解决的实际问题,那么你可能会提到实际问题所以我们可以(可能)展示你真的不想在那里使用operator *() 。

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

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