Access Members of a struct using Array Syntax?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • berrylthird
    New Member
    • Jan 2008
    • 2

    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:

    [CODE=javascript]var myInstance:myCl ass = new myClass();

    myInstance.memb er_0 = memberValue_0; // absolute notation
    myInstance[0] = memberValue_0; // relative notation
    myInstance["member_0"] = memberValue_0; // nominal notation[/CODE]
    You can initialize a struct in C/C++ like you would an array, as with the following code:

    [CODE=c]struct myClass {
    int member_0;
    int member_1;
    int member_2;
    }

    myClass myInstance = {memberValue_0, memberValue_1, memberValue_2};[/CODE]
    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?:

    [CODE=c]struct myClass {
    int member_0;
    int member_1;
    int member_2;
    } myInstance;

    myInstance.memb er_0 = memberValue_0; // proper syntax
    myInstance[0] = memberValue_0; // legal?
    myInstance["member_0"] = memberValue_0; // legal?[/CODE]
    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.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Originally posted by berrylthitd
    struct myClass {
    int member_0;
    int member_1;
    int member_2;
    } myInstance;

    myInstance.memb er_0 = memberValue_0; // proper syntax
    myInstance[0] = memberValue_0; // legal?
    myInstance["member_0"] = memberValue_0; // legal?
    Let's go one at a time:
    [code=cpp]
    myInstance[0] = memberValue_0; // legal?
    [/code]

    Maybe. Is there a function to ovarload the assignment operator:
    [code=cpp]
    MyClass operator=(Mycla ss& lhs, MyClass& rhs);
    [/code]
    ???

    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:
    [code=cpp]
    myInstance["member_0"] = memberValue_0; // legal?
    [/code]

    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.

    Comment

    • berrylthird
      New Member
      • Jan 2008
      • 2

      #3
      I was thinking in the case that an array of structs were declared, i.e.:

      [CODE=c]myClass myInstance[totalInstances];
      [/CODE]
      .....then myInstance[0] could be legal. But even then, to reference myInstance[0] without the dot operator to explicitly call a member, i.e.:

      [CODE=c]myInstance[0] = memberValue_0;[/CODE]
      .....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.

      Comment

      Working...