By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,132 Members | 1,425 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,132 IT Pros & Developers. It's quick & easy.

Access Members of a struct using Array Syntax?

P: 2
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
Share this Question
Share on Google+
2 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
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

P: 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.