Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/142.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 子类方法指针,指向方法父参数c++;_C++_Class_Inheritance_Methods_Arguments - Fatal编程技术网

C++ 子类方法指针,指向方法父参数c++;

C++ 子类方法指针,指向方法父参数c++;,c++,class,inheritance,methods,arguments,C++,Class,Inheritance,Methods,Arguments,我想看到的可能很奇怪,我会尽可能地澄清。我在ubuntu 14.04和C++11上使用GCC4.8 我想尝试做的是: 上甲级 在类a中创建一个函数,该类作为参数获取 指向同一类的类成员的指针 创建从a继承的新类B 做一个B类的新方法 将指向类B的方法的指针指定给父类a的方法作为参数 class A{ typedef void(A::*METHOD); void executeMethod(METHOD arg){}; } class B : A{

我想看到的可能很奇怪,我会尽可能地澄清。我在ubuntu 14.04和C++11上使用GCC4.8

我想尝试做的是:

  • 上甲级
  • 在类a中创建一个函数,该类作为参数获取
  • 指向同一类的类成员的指针
  • 创建从a继承的新类B
  • 做一个B类的新方法
  • 将指向类B的方法的指针指定给父类a的方法作为参数

    class A{
        typedef void(A::*METHOD);        
    
        void executeMethod(METHOD arg){};
    }
    
    class B : A{
    
        void sampleMethod(){};
    
        void childMethod(){              
    
          this->executeMethod(&B::sampleMethod); //<== error
        }
    }
    
    这有什么办法吗?
    还有什么我需要做的来让你明白我想做什么吗?

    问题是
    sampleMethod
    不是
    a
    的成员,它是
    B
    的成员,不能转换为
    无效(a::*)

    你有没有考虑过使用虚拟方法?

    typedef void(A::*METHOD);        
    
    METHOD
    定义为指向
    a
    的成员变量的类型为
    void*
    的指针,而不是指向
    a
    的成员函数的指针

    你需要:

    typedef void (A::*METHOD)();        
    
    即使进行了这样的更改,您也不能使用
    B
    的成员函数作为参数传递,其中
    METHOD
    是预期的

    您可以使要传递的函数成为
    a
    的虚拟成员函数并使用它

    class A {
       protected:
          typedef void(A::*METHOD)();        
    
          void executeMethod(METHOD arg){};
    
       public:
          virtual void sampleMethod() = 0;
    };
    
    class B : public A {
    
       virtual void sampleMethod(){};
    
       void childMethod(){              
    
          this->executeMethod(&A::sampleMethod);
       }
    };
    

    要将可调用对象绑定到成员函数,应该使用std::bind。这将创建一个转发包装器,该包装器将调用sampleMethod并预先指定其一些参数。在这种情况下,“this”参数将被绑定


    std::bind(&B::sampleMethod,this)

    不能直接从基类调用子方法,但可以使用模板:

    class A {
    public:
        template<class T>
        void executeMethod( void (T::*method)() )
        {
            (static_cast<T *>( this )->*method)();
        }
    };
    
    class B : public A {
    public:
        void sampleMethod() {}
        void childMethod() { executeMethod( &B::sampleMethod ); }
    };
    

    首先,OP不需要函数指针,他要求一个指向方法的指针。第二个
    std::bind()
    将不会生成函数指针。已同意。我的术语很松散。我将编辑以更正措辞。typedef std::function方法;给我错误:“函数”未命名type@philsegeler您需要包括
    class A {
    public:
        template<class T>
        void executeMethod( void (T::*method)() )
        {
            (static_cast<T *>( this )->*method)();
        }
    };
    
    class B : public A {
    public:
        void sampleMethod() {}
        void childMethod() { executeMethod( &B::sampleMethod ); }
    };
    
    class A {
    public:
        typedef std::function<void()> Method;
    
        void executeMethod( const Method &method )
        {
            method();
        }
    };
    
    class B : public A {
    public:
        void sampleMethod1() {}
        void sampleMethod2( int param ) {}
    
        void childMethod1() { executeMethod( std::bind( &B::sampleMethod1, this ); }
        void childMethod2() { executeMethod( std::bind( &B::sampleMethod2, this, 123 ); }
    };