search

Home  >  Q&A  >  body text

C++类成员能当做变量类型吗?

  1. 弱逼问题:S::*xmeth是个啥? S是个类吧,xmeth是类的一个成员?成员能当做变量类型吗?

  2. 贴上相关代码

template<class S, class A1, class A2, class R> void
rpcs::reg(unsigned int proc, S*sob, int (S::*meth)(const A1 a1, const A2 a2, 
            R & r))
{
    class h1 : public handler {
        private:
            S * sob;
            int (S::*meth)(const A1 a1, const A2 a2, R & r);
        public:
            h1(S *xsob, int (S::*xmeth)(const A1 a1, const A2 a2, R & r))
                : sob(xsob), meth(xmeth) { }
            int fn(unmarshall &args, marshall &ret) {
                A1 a1;
                A2 a2;
                R r;
                args >> a1;
                args >> a2;
                if(!args.okdone())
                    return rpc_const::unmarshal_args_failure;
                int b = (sob->*meth)(a1, a2, r);
                ret << r;
                return b;
            }
    };
    reg1(proc, new h1(sob, meth));
}
  1. 贴上报错信息

  2. 贴上相关截图

  3. 已经尝试过哪些方法仍然没解决(附上相关链接)

PHP中文网PHP中文网2885 days ago650

reply all(3)I'll reply

  • 天蓬老师

    天蓬老师2017-04-17 13:24:27

    What is S::*xmeth? S is a class, right? xmeth is a member of the class?

    According to template parameters

    template<class S, class A1, class A2, class R>
    

    It can be seen that S, A1, A2 and R are all classes. So,

    S::*       // 表示指向类 S 的成员的指针
    S::*xmeth  // 表示该指针的变量名是 xmeth
    
    Can

    members be used as variable types?

    No, the type of the third parameter of function reg is member function pointer, not a member.

    Specifically, the variable name of the third parameter of function reg is meth and the type is int (S::*)(const A1, const A2, R &). That is, meth is a pointer to a member function of class S. The member function of this class S has three parameters. The types of these three parameters are const A1, const A2 and R & respectively. This The return value type of member functions of type S is int.

    might be easier to understand in English: meth is a pointer points to member function of class S whose parameters are const A1, const A2 and R &, return type is int.

    If you don’t understand it well, you can learn it first How to read function pointer, you can also use the cdecl website.

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-17 13:24:27

    int (S::*meth)(const A1 a1, const A2 a2, R & r);
    //这是你这个模板中的成员变量的声明
    //变量名字是meth
    //类型是 int (S::*)(const A1, const A2, R&), 也就是一个函数指针
    //不过 比较特别的是一个 类的成员函数 指针,不是普通的全局函数指针
    // 

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-17 13:24:27

    This is a member function pointer. Generally speaking, the length ranges from 4byte to 16byte. Internally, according to different ABI conventions, it describes the calling addressing method of member functions given a class pointer. I look at your class. There is also an S* member. This class should be a wrapper for class member functions

    The calling method in your code is like this (sob->*meth) (parameter)

    reply
    0
  • Cancelreply