Glib::RefPtr allows dereferencing via '->
' but not via '*
'. 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. 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, i.e. pass-by-reference.
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?
operator*()
?auto_ptr
,unique_ptr
andshared_ptr
all have that operator, andweak_ptr
only lacks it because it's not meant to be dereferencable. – Vesicantglibmm
development branchmaster
,Glib::RefPtr
is-astd::shared_ptr
, so it hasoperator*()
. Reference counting is thus done by the stdlib, not by GLib. – Editor