user1799323
user1799323

Reputation: 659

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. 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.

Upvotes: 0

Views: 468

Answers (3)

01d55
01d55

Reputation: 1912

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);

Upvotes: 0

IronMensan
IronMensan

Reputation: 6821

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.

Upvotes: 1

jtv4k
jtv4k

Reputation: 224

What you're trying to do is called reflection (Wikipedia). Since C++ is a compiled, non-managed language it doesn't support reflection.

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.

Unfortunately, when you're trying to accomplish by checking the name of a variable can't be done in C++.

Upvotes: 0

Related Questions