使用指向成员函数的指针在另一个类内部调用类构造函数

问题描述 投票:1回答:2

我正在尝试在另一个类的成员函数中初始化一个类的对象。问题是我需要将函数指针传递给构造函数。我不知道该怎么做。这是错误:

no matching function for call to ‘inheritance01::inheritance01(double (inheritance02::*&)(double))’
     inheritance01 LT (func);

下面的代码显示了问题。

class Base01 {
public:
    Base01(double (*)(double));
    virtual double calc(double) = 0;
    double (*ptr_fd() const)(double) { return ptr_fd_; }
private:
    double (*ptr_fd_)(double);
};

Base01::Base01(double (*f)(double))
    : ptr_fd_(f)
{
}
//----------------------------------------------------
class inheritance01 : public Base01 {
public:
    inheritance01(double (*ptr_f)(double));
    virtual double calc(double);
};

inheritance01::inheritance01(double (*pf)(double))
    : Base01(pf)
{
}

double inheritance01::calc(double t) { return 2.0 * t; }
//###################################################

class Base02 {
public:
    Base02(double);
    virtual double solution(double, double) = 0;
    double a() { return a_; };
private:
    const double a_;
};
Base02::Base02(double aa)
    : a_(aa)
{
}
//------------------------------------------------------
class inheritance02 : public Base02 {
public:
    inheritance02(double, double);
    virtual double solution(double, double);
    //static double sol_aux (double);
private:
    double sol_aux(double);
    const double b;
    //double (inheritance02::*fptrsol_aux)(double u) = &inheritance02::sol_aux;
    typedef double (inheritance02::*fptr)(double u);
    fptr func;
};

inheritance02::inheritance02(double aa, double bb)
    : Base02(aa)
    , b(bb)
{
    //func = double (*sol_aux)(double);
    //func = &inheritance02::sol_aux;
}
//--------------------------------------------------
double inheritance02::sol_aux(double u)
{
    return (a() + b) / u;
}
//--------------------------------------------------
double inheritance02::solution(double x, double t)
{
    //inheritance01 LT (&func);
    //inheritance01 LT (this->func);
    //inheritance01 LT (&fptrsol_aux);
    inheritance01 LT(func); // Here is the problem
    return LT.calc(x + t);
}
//########################################################
#include <iostream>
int main()
{
    inheritance02 obj(1.0, 1.0);
    double value = obj.solution(1.0, 1.0);
    std::cout << "value = " << value << std::endl;
    return 0;
}
c++ pointers
2个回答
0
投票

正如@Eljay的评论所说,您正在为指向成员函数的指针创建一个typedef:

typedef double (inheritance02::*fptr)(double u);

但是,inheritance01的构造函数将常规函数指针作为参数:

inheritance01( double (*ptr_f)(double));

所以这行:

inheritance01 LT(func); // Here is the problem

因为类型不匹配而无法使用(指向成员函数的指针不能转换为函数指针)。

简单的解决方法是使func成为常规函数指针,如下所示:

typedef double (*fptr)(double u);

并且一切都应该正常工作。

这里是demo


0
投票

尽管我无法将'func_'参数声明为const和private(我不知道如何返回std :: function,但是以下代码解决了我的问题:

#include <iostream>
#include <functional>
using namespace std;
class Base01
{
public:
    Base01( std::function<double (double)> );
    virtual double calc( double ) = 0;
    //function<double (double)> func { return func_; }
    function<double (double)> func;
private:
    //const function<double (double)> func_;   
};
Base01::Base01( function<double (double)> f) : func(f) {}
//----------------------------------------------------
class inheritance01:public Base01
{
public:
    inheritance01( function<double (double)> );
    virtual double calc( double ); 
};

inheritance01::inheritance01 (function<double (double)> f): Base01(f){}

double inheritance01::calc(double t) { return Base01::func(2.0*t); }
//###################################################
class Base02
{
public:
Base02(double);   
virtual double solution(double, double) = 0;
double a(){return a_;};
private:
const double a_;
};
Base02::Base02(double aa): a_(aa) {}
//------------------------------------------------------
class inheritance02 : public Base02
{
public:
    inheritance02( double, double );    
    virtual double solution(double, double);   
private:
    double sol_aux (double);
    const double b;
}; 

inheritance02::inheritance02 (double aa, double bb) 
: Base02(aa), b(bb)
{}
//--------------------------------------------------
double inheritance02::sol_aux(double u) { return (a()+b)/u; }
//--------------------------------------------------
double inheritance02::solution(double x, double y)
{
inheritance01 LT ( bind( &inheritance02::sol_aux, this, placeholders::_1) );
return LT.calc(x+y);
}
//########################################################
int main()
{
inheritance02 obj (1.0,1.0);
double value = obj.solution(1.0,1.0);
std::cout << "value = " << value << std::endl;
return 0;
}    
© www.soinside.com 2019 - 2024. All rights reserved.