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. accessing the elements of a struct from another class

accessing the elements of a struct from another class

Scheduled Pinned Locked Moved C / C++ / MFC
questiondockerhelp
5 Posts 2 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.
  • J Offline
    J Offline
    johnstonsk
    wrote on last edited by
    #1

    I know this is a studid question, but I am only able to create an instance of a struct. I actually need an instance of the struct container I have created a struct in class RFMAccess and I'm trying to get the elements from it in another class. struct TSimHeader { char Name[45]; char Unit[45]; double Min[45]; double Max[45]; int SignalCount; int SimStatus; }static TSimHeader_arr[10]; I can get them if I access them with the class name. **RFMAccess::TSimHeader_arr[j].Name[i];** I really don't want to have to type all of this so is there a way that I can create and instance of the container? so I coule type **s[j].Name[i]** I know, I know you guys feel like I have had the same kind of question for a week now. Sorry, but I keep changing things around. thanks for the help, steven

    D 1 Reply Last reply
    0
    • J johnstonsk

      I know this is a studid question, but I am only able to create an instance of a struct. I actually need an instance of the struct container I have created a struct in class RFMAccess and I'm trying to get the elements from it in another class. struct TSimHeader { char Name[45]; char Unit[45]; double Min[45]; double Max[45]; int SignalCount; int SimStatus; }static TSimHeader_arr[10]; I can get them if I access them with the class name. **RFMAccess::TSimHeader_arr[j].Name[i];** I really don't want to have to type all of this so is there a way that I can create and instance of the container? so I coule type **s[j].Name[i]** I know, I know you guys feel like I have had the same kind of question for a week now. Sorry, but I keep changing things around. thanks for the help, steven

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      No, not unless the other class was derived from RFMAccess. The statement s[j].Name[i] tries to resolve 's' to the calling class or any class it is derived from. Even if this other class were made a friend class of RFMAccess, it still wouldn't help. This designation simply allows access to private and protected members, but you'd still have to qualify them.

      J 1 Reply Last reply
      0
      • D David Crow

        No, not unless the other class was derived from RFMAccess. The statement s[j].Name[i] tries to resolve 's' to the calling class or any class it is derived from. Even if this other class were made a friend class of RFMAccess, it still wouldn't help. This designation simply allows access to private and protected members, but you'd still have to qualify them.

        J Offline
        J Offline
        johnstonsk
        wrote on last edited by
        #3

        So, your saying that I can't make and instance of the struct container? steven

        D 1 Reply Last reply
        0
        • J johnstonsk

          So, your saying that I can't make and instance of the struct container? steven

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          Saying "struct container" is redundant as a struct is a container of (different) data types. That aside, I'm not sure what you are asking. Your RFMAccess class has a TSimHeader structure in it with 10 instances defined. In order to access any one instance, you'll need to use square brackets like you've shown. It makes no difference whether you use TSimHeader_arr[i], or s[i]. Here's a kludgy example:

          class MyOtherClass
          {
          // a pointer to a single instance
          struct RFMAccess::TSimHeader *ts;

          void foo( int x )
          {
              // point to the instance
              ts = &(RFMAccess::TSimHeader\_arr\[x\]);
          }
          

          };

          You could also add the following function to your RFMAccess class:

          static struct TSimHeader *GetSingleInstance( int x )
          {
          return (&TSimHeader_arr[x]);
          }

          And use it in another class like:

          void foo2( void )
          {
          struct RFMAccess::TSimHeader *ts;
          ts = RFMAccess::GetSingleInstance(0);
          }

          Notice that qualification is still necessary for both usages.

          J 1 Reply Last reply
          0
          • D David Crow

            Saying "struct container" is redundant as a struct is a container of (different) data types. That aside, I'm not sure what you are asking. Your RFMAccess class has a TSimHeader structure in it with 10 instances defined. In order to access any one instance, you'll need to use square brackets like you've shown. It makes no difference whether you use TSimHeader_arr[i], or s[i]. Here's a kludgy example:

            class MyOtherClass
            {
            // a pointer to a single instance
            struct RFMAccess::TSimHeader *ts;

            void foo( int x )
            {
                // point to the instance
                ts = &(RFMAccess::TSimHeader\_arr\[x\]);
            }
            

            };

            You could also add the following function to your RFMAccess class:

            static struct TSimHeader *GetSingleInstance( int x )
            {
            return (&TSimHeader_arr[x]);
            }

            And use it in another class like:

            void foo2( void )
            {
            struct RFMAccess::TSimHeader *ts;
            ts = RFMAccess::GetSingleInstance(0);
            }

            Notice that qualification is still necessary for both usages.

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

            OK I see what you are saying, Thanks, steven

            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