简体   繁体   English

将void指针和结构传递给函数-c ++

[英]Passing void pointer and structures to functions -c++

I have a function that takes as a parameter a pointer to void. 我有一个函数,将指向空的指针作为参数。 So I pass to this function a struct, but inside that function I call another function (f_pD) that takes as a parameter a structure (struct). 因此,我将一个结构传递给该函数,但是在该函数内部,我调用了另一个将结构(结构)作为参数的函数(f_pD)。 Inside of the function f_mD1, I reset my structure and then I call to f_pD function that takes as parameter a struct. 在函数f_mD1内,我重置结构,然后调用以结构为参数的f_pD函数。 I do not want to have to reset my structure once I've done it in the main function. 在主要功能中完成结构后,我不需要重置结构。 Can anyone help me? 谁能帮我? A sketch of the code is below. 下面是代码的草图。 enter code here

    #include <fstream> 
    #include <map>
    using namespace std;

    struct pr_pD {
        int last;   double xD;  map <double, double> tab_w;
    } geral;

    double f_pD  (double, double, double, double, struct pr_pD geral);
    double f_mD1 (double, double, double, double, struct pr_pD geral);

    static int Integrand_pD(const int *ndim, const double k[], const int *ncomp, double FG[], void *parametros) 
    {   
        struct pr_pD * fp = (struct pr_pD *)parametros;             
        FG[0] = fp->last*fp->xD;       
    }

 static int Integrand_mD1(const int *ndim, const double k[], const int *ncomp, double F[], void *parametros) 
{   
    struct pr_pD * fp = (struct pr_pD *)parametros; 

    // ------------------------------------------- 
    // I want to eliminate this part of the code
    struct pr_pD geral;

    geral.tab_w = fp->tab_w;
    geral.last  = fp->last; 
    geral.xD    = fp->xD;       
    //----------------------------------------------
    double pD;

    pD = f_pD( fp->xD*k[1], fp->last*k[2], k[3], k[0], geral ); 

    F[0] = 2*pD;    

}
int main()
{   
    tab_w.insert(pair<double, double>( 1 , 2 ) );   
    geral.tab_w =  tab_w;       
    geral.last     = 1; 
    geral.xD       = 2.5;


    double mD1
    mD1 = f_mD1(1, 1, 1, 0.1, geral );

    return 0;   
}

double f_pD(double xD, double yD, double zD, double tD, struct pr_pD geral)
{   
    double val_int; 
    val_int = Cuhre(Integrand_pD, xD, yD, zD, tD);  
    return geral.last*val_int;
}

double f_mD1(double xD, double yD, double zD, double tD, struct pr_pD geral)
{   
    double val_int;
    val_int=Vegas(Integrnd_mD1, xD, yD, zD, tD);
    return geral.last*val_int;      
}

You can use *fp instead of the geral parameter, and just omit the code you say you want to get rid of. 您可以使用* fp而不是geral参数,而只需省略您想摆脱的代码。

pD = f_pD( fp->xD*k[1], fp->last*k[2], k[3], k[0], *fp ); 

By the way, these functions do not modify their geral parameter, so could accept that parameter by reference, avoiding a potentially expensive copy: 顺便说一下,这些函数不会修改其geral参数,因此可以通过引用接受该参数,从而避免了潜在的昂贵复制:

double f_pD(double xD, double yD, double zD, double tD, const pr_pD& geral)

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

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