468,510 Members | 1,619 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,510 developers. It's quick & easy.

Access Members of a struct using Array Syntax?

This question was inspired by scripting languages such as JavaScript. In JavaScript, I can access members of a class using array syntax as in the following example:

Expand|Select|Wrap|Line Numbers
  1. var myInstance:myClass = new myClass();
  2.  
  3. myInstance.member_0 = memberValue_0; // absolute notation
  4. myInstance[0] = memberValue_0; // relative notation
  5. myInstance["member_0"] = memberValue_0; // nominal notation
You can initialize a struct in C/C++ like you would an array, as with the following code:

Expand|Select|Wrap|Line Numbers
  1. struct myClass {
  2.      int member_0;
  3.      int member_1;
  4.      int member_2;
  5. }
  6.  
  7. myClass myInstance = {memberValue_0, memberValue_1, memberValue_2};
So my question is: Is it possible to access the members of a struct using the array syntax in the same manner as in the following?:

Expand|Select|Wrap|Line Numbers
  1. struct myClass {
  2.      int member_0;
  3.      int member_1;
  4.      int member_2;
  5. } myInstance;
  6.  
  7. myInstance.member_0 = memberValue_0; // proper syntax
  8. myInstance[0] = memberValue_0; // legal?
  9. myInstance["member_0"] = memberValue_0; // legal?
This question is purely academic and serves only my personal curiosity. I haven't worked in C or C++ for over a decade now, and I do not have access to a compiler to verify my question.
Jan 9 '08 #1
2 4853
weaknessforcats
9,207 Expert Mod 8TB
struct myClass {
int member_0;
int member_1;
int member_2;
} myInstance;

myInstance.member_0 = memberValue_0; // proper syntax
myInstance[0] = memberValue_0; // legal?
myInstance["member_0"] = memberValue_0; // legal?
Let's go one at a time:
Expand|Select|Wrap|Line Numbers
  1. myInstance[0] = memberValue_0; // legal?
  2.  
Maybe. Is there a function to ovarload the assignment operator:
Expand|Select|Wrap|Line Numbers
  1. MyClass operator=(Myclass& lhs, MyClass& rhs);
  2.  
???

If there isn't then this is not legal. If there is the compiler will call it to assign the values to myInstance[0].

Then with:
Expand|Select|Wrap|Line Numbers
  1. myInstance["member_0"] = memberValue_0; // legal?
  2.  
As the struct is defined, no.

However, you could write a container struct that contains the array of MyClass and overloads the index operator to look up an object by name.
Jan 9 '08 #2
I was thinking in the case that an array of structs were declared, i.e.:

Expand|Select|Wrap|Line Numbers
  1. myClass myInstance[totalInstances];
  2.  
.....then myInstance[0] could be legal. But even then, to reference myInstance[0] without the dot operator to explicitly call a member, i.e.:

Expand|Select|Wrap|Line Numbers
  1. myInstance[0] = memberValue_0;
.....would it default to the first memory location of the first member of the struct?

Apparently, the answer is NO.

Thank you for your response! I've been coding in scripting languages for so long it would be nice to get back down to some low-level coding for a change.
Jan 9 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

12 posts views Thread by James Brown | last post: by
5 posts views Thread by jimjim | last post: by
9 posts views Thread by gk245 | last post: by
reply views Thread by Adam Warner | last post: by
9 posts views Thread by sean.scanlon | last post: by
6 posts views Thread by zfareed | last post: by
6 posts views Thread by Urs Thuermann | last post: by
6 posts views Thread by noone | last post: by
reply views Thread by NPC403 | last post: by
3 posts views Thread by gieforce | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.