C ++中的嵌套函数

时间:2015-10-01 09:55:39

标签: c++ c++11

我希望在C ++中使用嵌套函数以避免全局变量。我通过使用类(http://www.gotw.ca/gotw/058.htm)找到了一种方法。我试图实现该解决方案,但我失败了。这是我的代码:

<script>
        // Set url
        var jsonUrl = 'myJson.json';

        // Call WinJS.xhr to retrieve a json file
        WinJS.xhr({
            url: jsonUrl,
            responseType: "json"
            // https://msdn.microsoft.com/en-us/library/windows/apps/br229787.aspx
            // json: The type of response is String. This is used to represent JSON strings. responseText is also of type String, and responseXML is undefined.
            // Note  As of Windows 10, when responseType is set to json, the responseText is not accessible and the response is a JavaScript object. Behavior on Windows 8.1 and earlier remains as described above.
        }).done(

            // When the result has completed, check the status.
            function completed(result) {
                if (result.status === 200) {

                    // Get the XML document from the results.
                    console.log(result);
                    var jsonFileContent = result.response; // returns an object
                    console.log(jsonFileContent);
                    console.log(jsonFileContent.rabbits[0].name);

                    /* The content of file: myJson.json
                    {
                        "rabbits":[
                            {"name":"Jack", "age":3},
                            {"name":"Mac", "age":4},
                            {"name":"Hanna", "age":2}
                        ]
                    }
                    */
                }
            });
    </script>

目标是返回一个原型为double(double,double)的函数,该函数将用于其他指令。

我收到了这条错误消息:

class Myclass 
{
    double (*retval)(double, double);
double A;
double x0, y0, z0, z, a, b,k;
int mu, nu, s, t;

double g(double x, double y)
{
    return (x0+y0+z0+a+b+k+s+t+mu+nu)*A*x*y;
}
public :
Myclass(complex<double> A, double x0, double y0, double z0, double k, double a, double b,  int mu, int nu, int s, int t) : z(0.)
{
    this->A=A;
    this->x0=x0;
    this->y0=y0;
    this->z0=z0;
    this->k=k;
    this->a=a;
    this->b=b;
    this->mu=mu;
    this->nu=nu;
    this->s=s;
    this->t=t;
    retval=g;
}
operator double (double, double)
{
    return retval;
}
};

int main ()
{
    /*
    ....
   */
    return 0;
}

1 个答案:

答案 0 :(得分:0)

  

目标是返回原型double(double,double)

的函数
operator double (double, double)

声明conversion operator返回函数的函数。

要退回功能,并且由于您使用标记了问题,我建议您使用std::function

std::function<double(double, double)> get() const
{
    return retval;
}

将函数指针返回到double(double, double)函数的语法如下所示:

double (*get())(double, double) const
{
    return retval;
}