How to get the name of a variable ?
-
wrote on 25 Dec 2018, 09:50 last edited by
Is there a way to get the name of a variable?
I want to make it a function.Below is the source code I tried.
QString hello = "World!"; qDebug() << getVarName(&hello); // I want result : "hello" #define VAR_TO_STR(name) #name QString getVarName(QString *var) { return VAR_TO_STR(var); }
I want code that can get the name and value of a variable at the same time.
-
wrote on 25 Dec 2018, 09:55 last edited by
Sorry, could I ask you why do you need this? Which is the situation that requires this?
-
wrote on 25 Dec 2018, 10:35 last edited by
I asked for convenience and curiosity.
It is not absolutely necessary.Below is the cause of this problem.
Setting::Setting(QWidget *parent) : QDialog(parent), ui(new Ui::Dialog) { ui->setupUi(this); settings = new QSettings("Setting.ini",QSettings::IniFormat); setDefault(&billacceptorPort,"BillDispensor","COM10"); } void Setting::setDefault(QString *var,QString name,QString val) { if(settings->value(name).toString()=="") settings->setValue(name,val); *var = settings->value(name).toString(); }
The setDefault function is simple, but iterative.
So I was thinking of a way to make it simpler. -
what is iterative inside setDefault function ? It is just one if condition. Did you miss something here ?
-
wrote on 25 Dec 2018, 11:26 last edited by
The function itself is not iterative, but the function is used repeatedly.
Even if I use the source that I created, it does not interfere with the operation.
However, you must use more than 100 'setDefault'.
I want to minimize the possibility of mistakes because I have little experience with the program.
-
wrote on 25 Dec 2018, 11:29 last edited by
Thank you for answer.
I will do it the before way if there is no way to get the name. -
No way to get the variable name. Also no use from getting the name of the variable. What matters is the value. You must work on the value rather than the variable name.
-
wrote on 18 Aug 2020, 06:50 last edited by
QDebug() << QT_STRINGIFY(MainWindowVM);
QDebug QDebug(nullptr); QDebug << QT_STRINGIFY(QDebug);