0

Suppose I have a very simple class:

class myClass {
public:
void printName();
};

void myClass::printName() {
std::cout << /*name of variable*/;
}

Now in my main file I do this:

#include <iostream>
#include "classHeader"
int main() {
myClass testvar;
/*calling testvar.printName() should print "testvar"*/
}

How do I do this? Thanks in advance.

avighnac
  • 376
  • 4
  • 12
  • 4
    Not possible. Variable names only exist at compile-time, not at runtime. – HolyBlackCat Nov 20 '21 at 14:33
  • Wait, seriously? There must be a way to do this. – avighnac Nov 20 '21 at 14:34
  • 2
    Nope, there is no way. – HolyBlackCat Nov 20 '21 at 14:34
  • 1
    One way is to have a member variable that is initialized with a string that is the same as the name of the class object. Another way is to leverage debug information to find the associated name of the variable. (That would be very platform specific.) – Eljay Nov 20 '21 at 14:34
  • @Eljay by that, do you mean a constructer function that takes in a string? – avighnac Nov 20 '21 at 14:37
  • The best you can do is get a function name. Most compilers have `__FUNCTION__` or `__func__` or `__function__`. Which is the name of the function this is put in. Instead of /*name of variable*/ put "name of variable". – QuentinUK Nov 20 '21 at 14:47
  • 2
    What you are looking for is relfection, and it is not supported in C++. (This is not .Net). C++ compiles to machine code so in the end only addresses of things are known. The closest things are debug databases for debug builds. So my question is WHY would you need those names anyway? – Pepijn Kramer Nov 20 '21 at 14:58
  • 1
    It would help if we knew what problem you were trying to solve. – Tanveer Badar Nov 20 '21 at 15:07
  • *"do you mean a constructer function that takes in a string"* Probably yes. Aka manually saving the name somewhere, then printing it. – HolyBlackCat Nov 20 '21 at 15:15
  • The reason I need this name is I'm trying to pass information between two .exe's and hence, am making a special kind of variable which saves its data to a file so it can be accessed by both of those files. This was just to make the saving to file bit a little easier. – avighnac Nov 20 '21 at 15:36
  • You may want to read about [The XY Problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). You are not asking how to solve a practical problem here. Instead, you are asking how to get your solution to a problem to work correctly. A different question may help you more. – Drew Dormann Nov 20 '21 at 17:46
  • Yes, a constructor taking in a string. For example, `myClass testvar{"testvar"};` – Eljay Nov 20 '21 at 17:52
  • 1
    @Drew Dormann you're right, this probably wasn't the best approach to do it. I'll ask a question on that then. – avighnac Nov 21 '21 at 18:43
  • @DrewDormann sorry for replying so late, but I didn't include many details because if I did, the question would be *very* specific to me, and then it's not going to be helpful to other people. – avighnac May 16 '22 at 17:08
  • @avighnac not a problem, and good luck! – Drew Dormann May 16 '22 at 17:45

0 Answers0