accessing the elements of a struct from another class
-
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 -
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, stevenNo, 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 afriend 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. -
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 afriend 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.So, your saying that I can't make and instance of the struct container? steven
-
So, your saying that I can't make and instance of the struct container? steven
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.
-
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.
OK I see what you are saying, Thanks, steven