简体   繁体   English

在C ++中将变量名转换为字符串?

[英]Converting a variable's name to a string in C++?

main function 主功能

main(){

    foo x=1, y=1, z=1;

}

header/class 标头/类

class foo{

public:
    double a, b, c;
    double fn, val;

    // set a,b, and c
    void set(){
        a=1;
        b=1;
        c=1;
    }

    // constructor
    foo(double &f){
        set();

        // what I want to do here is say if "name of variable f" = "x", then do something
        // else if "name of variable f" = "y", do something else
        // else if "name of variable f" = "z", do something else
    }
};

As you can see in the main function, x, y and z all have the same value. 正如您在主函数中看到的那样,x,y和z都具有相同的值。 I'm trying to write a conditional that deals with this case and one way I came up with it to check the variable names. 我试图编写一个处理这种情况的条件,并且我想出了一种方法来检查变量名。 Because I ALWAYS want to do the same thing for the object of class foo named "x", and always do the same thing for "y" and so on, no matter what those values happen to be. 因为我总是要对名为“ x”的foo类的对象执行相同的操作,并且始终对“ y”等执行相同的操作,依此类推,无论这些值发生了什么。

You can't do what you are asking for in your question, there are a couple of ways to do something similar. 您无法按照自己的要求去做,有两种方法可以做类似的事情。

You can use inheritance. 您可以使用继承。

class foo{

public:
    double a, b, c;
    double fn, val;

    // set a,b, and c
    void set(){
        a=1;
        b=1;
        c=1;
    }

    // constructor
    foo(double &f){
        set();
    }
};

class X : public foo {
{
public:
    X (double &f) : foo(f) {
        // do stuff for x
    }
};

class Y : public foo {
{
public:
    Y (double &f) : foo(f) {
        // do stuff for y
    }
};

class Z : public foo {
{
public:
    Z (double &f) : foo(f) {
        // do stuff for z
    }
};

main(){

    X x=1;Y y=1;Z z=1;

}

Or you can use an enumeration 或者您可以使用枚举

class foo{
public:
    enum Mode{
        Mode_X,
        Mode_Y,
        Mode_Z
    };
    Mode mode;

    double a, b, c;
    double fn, val;

    // set a,b, and c
    void set(){
        a=1;
        b=1;
        c=1;
    }

    foo(Mode m, double &f) : mode(m) {
        set();

        switch(mode) {
        case Mode_X:
            // what I want to do here is say if "name of variable f" = "x", then do something
            break;
        case Mode_Y:
            // else if "name of variable f" = "y", do something else
            break;

        case Mode_Z:
            // else if "name of variable f" = "z", do something else
            break;
        }
    }
};

main(){

    foo x(foo::Mode_X,1), y(foo::Mode_Y,1), z(foo::Mode_Z,1);

}

You can use the preprocessor with the enumeration version to get the variable declaration closer to what you were originally asking for like this: 您可以使用带有枚举版本的预处理器,以使变量声明更接近您的原始要求,如下所示:

#define X(value) x(foo::Mode_X,(value))
#define Y(value) y(foo::Mode_Y,(value))
#define Z(value) z(foo::Mode_Z,(value))

main(){
    foo X(1), Y(1), Z(1);
}

Many people, myself included, would advise against using the preprocessor like this. 许多人,包括我自己在内,都建议不要使用这种预处理器。 I am only saying that it is possible. 我只是说有可能。

What you're trying to do is called reflection ( Wikipedia ). 您尝试做的事情称为反射( Wikipedia )。 Since C++ is a compiled, non-managed language it doesn't support reflection. 由于C ++是一种编译的非托管语言,因此不支持反射。

Also, when code is compiled in C++, the compiler mangles the variable names so the variables that you think you created (x, y, z) aren't named at all what you think they are and the new names have no meaning. 同样,当代码用C ++编译时,编译器会处理变量名,因此您认为自己创建的变量(x,y,z)根本不会像您想的那样命名,而新名称毫无意义。

Unfortunately, when you're trying to accomplish by checking the name of a variable can't be done in C++. 不幸的是,当您试图通过检查变量名来完成操作时,在C ++中无法完成。

You mention in comments that you're writing a class to do partial differentiation. 您在注释中提到您正在编写一个进行部分区分的类。 Here's a suggested starting point: 这是一个建议的起点:

class Differentiator{
public:
    double a, b, c;
    double fn, val;
    void differentiateByX(double &f);
    void differentiateByY(double &f);
    void differentiateByZ(double &f);

    Differentiator(): a(1), b(1), c(1)
    {} // Note the syntax above for initializing members.
};

If it seems useful, feel free to change the return type of the differentiate functions, or to add members so that you can do 如果看起来有用,请随时更改区别函数的返回类型,或添加成员,以便您可以

main(){
    Differentiator foo;
    foo.differentiateByX(1);
    // do something with the result
    foo.differentiateByY(2);
    // etc.
}

If you know you're always going to want to differentiate by X, Y, and Z, you could have a single Differentiator do all three with a single function, differentiate(double &x, double &y, double &z) or go back to doing all the work in your constructor: Differentiator foo(x, y, z); 如果您知道总是要用X,Y和Z进行Differentiator ,则可以让一个Differentiator器用一个函数完成所有三个操作,然后进行differentiate(double &x, double &y, double &z)或返回执行构造函数中的所有工作: Differentiator foo(x, y, z);

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

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