class name into char

Hi,

I don't think i know enough C++ to solve the following problem:
I call a class:
States eiStates(coef1,space);
this class outputs into a txt file.
is there anyway for that file to have the same name as the class + ".dat" extension: "eiStates.dat"?
so this class will output eiStates.dat
and after i call another class
States uStates(coef2,space);
i would like that class to automatically output uStates.dat

thanks you,
--Kirill
No, you have to pass the name of the txt file explicitly, which might happen to be the same as your variable's name.
If I understand OP correctly.... you want to have the name of the object not the name of the class.

ie:

1
2
3
MyClass foo;
// you want "foo.dat"
//   not "MyClass.dat" 


In which case, R0mai is correct. There's no way to get the name of the variable at runtime because variable names don't exist at runtime -- they're just there to make writing code easier, but they typically don't exist in the executable (except for debugging purposes, but nevermind that).

The only solution I see to this problem is to record the name of the object in the object itself:

 
Sates eiStates(coef1,space,"eiStates.dat");  // note 3rd param 
You can use the preprocessor for this:
1
2
3
4
#define GetName(var) std::string(#var)
//...
States eiStates(coef1,space);
fstream file ( GetName(eiStates)+".dat" );
Is putting the variable in a macro really any easier than just putting it in quotes?

Besides... I just assumed he wouldn't have the name of the variable at the time (like he's doing it via pointer/reference in another function or something).
Topic archived. No new replies allowed.