如何重载 - > *运算符? [英] How to overload the ->* operator?

查看:119
本文介绍了如何重载 - > *运算符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试过这个,以及它的一些变化:

 模板< class T& 
class Ptr {
public:
Ptr(T * ptr):p(ptr){}
〜Ptr(){if(p)delete p; }

template< class Method>
方法运算符 - > *(方法方法)
{
return p-> * method;
}

private:
T * p;
};

类Foo {
public:
void foo(int){}
int bar(){return 3; }
};

int main(){
Ptr< Foo> p(new Foo());

void(Foo :: * method)(int)=& Foo :: foo;
int(Foo :: * method2)()=& Foo:bar;

(p-> * method)(5);
(p-> * method2)();

return 0;
}

但它无法使用。问题是,我真的不知道期望什么作为参数或返回什么。这个标准对我来说是不可理解的,因为谷歌没有提供任何有用的东西我想我不是孤单。



编辑:另一种尝试,使用C ++ 0x: http://ideone.com/lMlyB

operator-> * 的返回表示被调用过程中的一个函数,只有缺少的部分是参数。因此,你必须返回一个函数,它使用给定的参数调用给定对象上的给定函数:

  // PTMF = pointer到成员函数
template< class Obj>
struct PTMF_Object {
typedef int(Obj :: * ptmf)(double,std :: string); // example signature

PTMF_Object(Obj * obj,ptmf func)
:obj_(obj)
,func_(func)
{}
b $ b int operator()(double d,std :: string str){
return(obj _-> * func _)(d,str);
}

Obj * obj_;
ptmf func_;
};

template< class T>
struct SmartPtr {
// ...

PTMF_Object< T> operator> *(PTMF_Object< T> :: ptmf func){
return PTMF_Object< T&
}
// ...
};

int main(){
SmartPtr< Foo> pf(new Foo());
typedef int(Foo :: * Foo_ptmf)(double,std :: string);
Foo_ptmf method =& Foo :: bar;

(pf-> * method)(5.4,oh hi);
}

Edit2

< a href =http://www.aristeia.com/Papers/DDJ_Oct_1999.pdf>这里是Scott Meyers在这个主题上的一个很好的pdf(它是有关重载 编辑
$ b $

b这是一个,如果您的编译器支持可变参数模板: http://ideone.com/B6kRF


I tried this, and some variations on it:

template<class T>
class Ptr {
public:
    Ptr(T* ptr) : p(ptr) {}
    ~Ptr() { if(p) delete p; }

    template<class Method>
    Method operator ->* (Method method)
    {
        return p->*method;
    }

private:
    T *p;
};

class Foo {
public:
    void foo(int) {}
    int bar() { return 3; }
};

int main() {
    Ptr<Foo> p(new Foo());

    void (Foo::*method)(int) = &Foo::foo;
    int (Foo::*method2)() = &Foo::bar;

    (p->*method)(5);
    (p->*method2)();

    return 0;
}

But it doesn't work. The problem is that I don't really know what to expect as a parameter or what to return. The standard on this is incomprehensible to me, and since Google did not bring up anything helpful I guess I'm not alone.

Edit: Another try, with C++0x: http://ideone.com/lMlyB

解决方案

The return of operator->* represents a function in the process of being called, with the only missing parts being the parameters. Thus, you must return a functor that invokes the given function on the given object with the given parameters:

// PTMF = pointer to member function
template<class Obj>
struct PTMF_Object{
  typedef int (Obj::*ptmf)(double,std::string); // example signature

  PTMF_Object(Obj* obj, ptmf func)
    : obj_(obj)
    , func_(func)
  {}

  int operator()(double d, std::string str){
    return (obj_->*func_)(d,str);
  }

  Obj* obj_;
  ptmf func_;
};

template<class T>
struct SmartPtr{
  // ...

  PTMF_Object<T> operator->*(PTMF_Object<T>::ptmf func){
    return PTMF_Object<T>(p, func);
  }
  // ...
};

int main(){
  SmartPtr<Foo> pf(new Foo());
  typedef int (Foo::*Foo_ptmf)(double,std::string);
  Foo_ptmf method = &Foo::bar;

  (pf->*method)(5.4, "oh hi");
}

Edit2
Here is an excellent pdf from Scott Meyers on this very topic (it's the only good literature on overloading operator->*, even though it's from 1999).

Edit
Here is one, if your compiler supports variadic templates: http://ideone.com/B6kRF

这篇关于如何重载 - &gt; *运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆