Access structure variable value using string representing variable's name in C++.
-
Dear Friends, I want to design function using C++. Like this int GetValue(CString VariName ); I will pass Variable name as a String into function and function should return value of the variable. struct StudentInfo { int rollnum; int mark1; int mark2; } *stud; int GetVal( _T("Stud->rollnum")); // Now function should return the value of Stud->rollnum Friends help me... Thanks and Regards, S.Shanmuga Raja
-
Dear Friends, I want to design function using C++. Like this int GetValue(CString VariName ); I will pass Variable name as a String into function and function should return value of the variable. struct StudentInfo { int rollnum; int mark1; int mark2; } *stud; int GetVal( _T("Stud->rollnum")); // Now function should return the value of Stud->rollnum Friends help me... Thanks and Regards, S.Shanmuga Raja
Answer is apart. Why you want to do it Like that?
-
Dear Friends, I want to design function using C++. Like this int GetValue(CString VariName ); I will pass Variable name as a String into function and function should return value of the variable. struct StudentInfo { int rollnum; int mark1; int mark2; } *stud; int GetVal( _T("Stud->rollnum")); // Now function should return the value of Stud->rollnum Friends help me... Thanks and Regards, S.Shanmuga Raja
C/C++ has no built-in mechanism to do that. you will have to come up with some kind of data structure which maps a string to a variable. maybe put the variable name in the structure itself, then search your collection of structs to find the one you want. or, create a std::map which uses a string (var name) as the key and a struct as the value. or... there are lots of options - none of them are great.
-
Dear Friends, I want to design function using C++. Like this int GetValue(CString VariName ); I will pass Variable name as a String into function and function should return value of the variable. struct StudentInfo { int rollnum; int mark1; int mark2; } *stud; int GetVal( _T("Stud->rollnum")); // Now function should return the value of Stud->rollnum Friends help me... Thanks and Regards, S.Shanmuga Raja
If you want to use c++ make a class instead of struct:
class CStudentInfo
{
public:
enum EVal
{
erollnumEVal,
emark1EVal,
emark2EVal
};int GetValue( EVal eVal )
{
int iRetVal = 0;switch( eVal )
{
case erollnumEVal:
iRetVal = rollnum;
break;case emark1EVal: iRetVal = mark1; break; case emark2EVal: iRetVal = mark2; break; default: ASSERT( FALSE ); break;
}
return iRetVal;
}private:
int rollnum;
int mark1;
int mark2;
}But why do you need something like this? couldn't it be just simple:
class CStudentInfo
{
public:int Getrollnum()
{
return rollnum;
}int Getmark1()
{
return mark1;
}int Getmark2()
{
return mark2;
}private:
int rollnum;
int mark1;
int mark2;
} -
Dear Friends, I want to design function using C++. Like this int GetValue(CString VariName ); I will pass Variable name as a String into function and function should return value of the variable. struct StudentInfo { int rollnum; int mark1; int mark2; } *stud; int GetVal( _T("Stud->rollnum")); // Now function should return the value of Stud->rollnum Friends help me... Thanks and Regards, S.Shanmuga Raja
-
Dear Friends, I want to design function using C++. Like this int GetValue(CString VariName ); I will pass Variable name as a String into function and function should return value of the variable. struct StudentInfo { int rollnum; int mark1; int mark2; } *stud; int GetVal( _T("Stud->rollnum")); // Now function should return the value of Stud->rollnum Friends help me... Thanks and Regards, S.Shanmuga Raja
Please do not cross-post! It just makes it harder for everyone to gather the sparse information that is already there[^], and makes people waste time repeating the same queastions and suggestions in two different places! Instead, please answer the questions, especially what you need this for. The answers to these questions are really necessary for us to make a meaningful response that actually helps you with your problem. As long as you're silent about your real intent, we can only guess! And, judging by the little information you gave us, you don't know what you're doing. Much less do we. Also, please respond to the suggestions already made, whether or not they fulfill your purpose. If you ask a question, you expect a response. Likewise, the people responding expect that you indicate when that wasn't helpful, and why. P.S.: Just to make a point and explain why I think you do not know what you'r doing or asking: The name of a variable in a program only really makes sense to the programmer who wrote it. The same object stored under that variable name may be stored under a different name in another part of the program. Likewise, a different part of the program may store a different object under the same name. As a result, the user of the program can never tell with certainty what the name of a specific object in the source code of the program at any given time is. the only way to tell this, is if you are looking at the source code that is currently executed, i. e. if you are debugging the code. Is that your purpose? If so, the task you're on is likely a whole lot more complex than you can imagine. If not, you're probably asking the wrong question, and you need something different than what you're asking for.