Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Access structure variable value using string representing variable's name in C++.

Access structure variable value using string representing variable's name in C++.

Scheduled Pinned Locked Moved C / C++ / MFC
c++designhelp
6 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    shanmugarajaa
    wrote on last edited by
    #1

    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

    S C N J S 5 Replies Last reply
    0
    • S shanmugarajaa

      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

      S Offline
      S Offline
      Sivaraman Dhamodharan
      wrote on last edited by
      #2

      Answer is apart. Why you want to do it Like that?

      Programming Article

      1 Reply Last reply
      0
      • S shanmugarajaa

        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 Offline
        C Offline
        Chris Losinger
        wrote on last edited by
        #3

        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.

        image processing toolkits | batch image processing

        1 Reply Last reply
        0
        • S shanmugarajaa

          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

          N Offline
          N Offline
          Newbie00
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • S shanmugarajaa

            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

            J Offline
            J Offline
            jschell
            wrote on last edited by
            #5

            Use a hash map. The name of the variable in the name of the entry. The value of the variable is the value of the entry.

            1 Reply Last reply
            0
            • S shanmugarajaa

              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

              S Offline
              S Offline
              Stefan_Lang
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups