简体   繁体   English

如何使用函数指针初始化类中的对象?

[英]How do I use function pointers to initialize an object inside a class?

I have 3 classes - a superclass, a subclass, and a container class. 我有3个类-超类,子类和容器类。 My superclass has the following constructor: 我的超类具有以下构造函数:

Superclass(char* t,int(*i)(myType),int a,int b,int c,int p){
    T=t;
    I=i;
    A=a;
    B=b;
    C=c;
    P=p;
}

And the subclass constructor: 和子类的构造函数:

Subclass(char* t,int(*i)(myType),int a,int b,int c,int p)
  : Superclass(t,i,a,b,c,p){;}

The container class contains multiple pointers to objects of the subclass type: 容器类包含多个指向子类类型的对象的指针:

class Container{
  public:
    char x[2000];
    int funct(myType);
    ...
    Subclass* S;
    ...
    Container(){
      S= new Subclass(x,&funct,3,4,2000,0);
      ...
    }
}

I get a compiler error on the above line "S= new ..." with the message: 我在上述消息“ S = new ...”上收到编译器错误:

"Error: No instance of 'Subclass::Subclass' matches the argument list
argument types are: (char[2000],int(Container::*)(myType),int,int,int,int)"

I believe (though I'm not certain) the error has something to do with the function pointer being passed. 我相信(尽管我不确定)该错误与传递的函数指针有关。 I've used function ptrs in a similar manner, but it seems to not like that its pointing to a function inside the Container class. 我以类似的方式使用了函数ptrs,但似乎不喜欢它指向Container类中的函数。 Any suggestions? 有什么建议么?

Thanks in advance for the help. 先谢谢您的帮助。

The function pointer you passed is a class member function, which is of type int(Container::*)(myType) , which causes the problem. 您传递的函数指针是类成员函数,其类型为int(Container::*)(myType) ,这会引起问题。

If the funct function doesn't need the information from a Container object, then you can make it static. 如果funct函数不需要来自Container对象的信息,则可以使其为静态。 If it indeed need the access to the object, then you either want to change the type of the parameter to std::function<int(Container&,myType)> and use this function with a object. 如果确实需要访问该对象,则可以将参数的类型更改为std::function<int(Container&,myType)>并将此函数与对象一起使用。 Or change it to std::function<int(int)> and pass the function through std::bind . 或将其更改为std::function<int(int)>并通过std::bind传递函数。

You can't pass a member function as a plain function pointer, unless it's a static member function. 您不能将成员函数作为普通函数指针传递,除非它是静态成员函数。

static int funct(myType);

Naturally a static function won't have access to member variables or functions that aren't also static, unless you use them in conjunction with another object pointer. 自然,静态函数将无法访问非静态的成员变量或函数,除非您将它们与另一个对象指针结合使用。

You could also change the type of the pointer passed to the constructor to be a member pointer, but that's much more involved and isn't generally useful unless you're also passing an object of that type. 您还可以将传递给构造函数的指针的类型更改为成员指针,但这要涉及得多,并且除非您还传递该类型的对象,否则通常没有用。

Subclass(char* t,Container* cp, int((Container::*)i)(myType),int a,int b,int c,int p)
  : Superclass(t,cp,i,a,b,c,p){;}

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

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