Thread: quicker access to structmembers?

  1. #1
    *******argv[] - hu? darksaidin's Avatar
    Join Date
    Jul 2003
    Posts
    314

    quicker access to structmembers?

    Hi,

    is there a way to quickly access members of a struct without naming the struct every single time?

    here's a code example of what I mean

    Code:
    struct myStruct {
      int ia, ib, ic, id, ie, ig;
      bool bIsItTrue;
    }
    Now to access some of the structs members I would need to do something like this:

    Code:
    doSomethingWithSomeStructMembers(myStruct.ia, myStruct.ie, myStruct.ig, myStruct.bIsItTrue);
    Is there any way I can tell the compliler that all of the following variables refer to the struct, if they match the name of a member of the struct ?
    What it could look like (this code does not work of course, I wouldn't ask if it did )

    Code:
    with myStruct {
      doSomethingWithSomeStructMembers(ia, ie, ig, bIsItTrue);
    }
    I did a search on this topic, but it's really hard to search something if you don't know it's name.

  2. #2
    Registered User newbie_grg's Avatar
    Join Date
    Jul 2002
    Posts
    77
    You really need to format your question.

  3. #3
    Grammar Police HybridM's Avatar
    Join Date
    Jan 2003
    Posts
    355
    you could just pass the whole struct to the function by reference. Then access the vairables inside the function as needed.
    Thor's self help tip:
    Maybe a neighbor is tossing leaf clippings on your lawn, looking at your woman, or harboring desires regarding your longboat. You enslave his children, set his house on fire. He shall not bother you again.

    OS: Windows XP
    Compiler: MSVC

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>is there a way to quickly access members of a struct
    The short answer is no, I believe. Passing the struct by reference is OK if that's what your implementation allows for, but sometimes you might find it more flexible to leave it as four seperate parameters. It's up to you...
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    I think Hybrid's answer is what you're looking for. On the other hand, this is the C++ forum and you should be using a class instead. This gives you an implied "this" pointer when you're inside a member function.

    so your call should be:

    myClass.DoSomething();

    instead of:

    DoSomethingWithStruct(&myStruct);
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  6. #6
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    I think references or pointers would work great here. It would certainly save time for coding. This is, of course, if you made the function, and can change it.

    with myStruct {
    doSomethingWithSomeStructMembers(ia, ie, ig, bIsItTrue);
    }
    Sniff.... sniff... I smell Visual Basic. I'm afraid that with C++, you sacrifice easy coding for powerful coding.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  7. #7
    *******argv[] - hu? darksaidin's Avatar
    Join Date
    Jul 2003
    Posts
    314
    Originally posted by newbie_grg
    You really need to format your question.
    Sorry, but I think my question is 100% clear (someone else correct me if I'm wrong ). The examples are not even needed to understand it, I just added them to make it easier to understand. "Is there a way to quickly access members of a struct without naming the struct every single time?" is accurate unless, which is possible, my grammar is somehow wrong.

    Originally posted by FillYourBrain
    I think Hybrid's answer is what you're looking for. On the other hand, this is the C++ forum and you should be using a class instead. This gives you an implied "this" pointer when you're inside a member function.

    so your call should be:

    myClass.DoSomething();

    instead of:

    DoSomethingWithStruct(&myStruct);
    The functioncall and the struct was only an example. I use structs to group options within a class so the code is easier to read. I don't want to define another "subclass" for a few options that will be used by virtually every method of the class.
    Even if I did, I would still have to name the subclasses name every single time when setting it's fields from the hostclass.
    In ObjectPascal, you could use the "with" keyword for structs or classes to get rid of this "overhead". Thats why I asked if C++ had something similar to offer.

    Originally posted by bennyandthejets
    Sniff.... sniff... I smell Visual Basic. I'm afraid that with C++, you sacrifice easy coding for powerful coding.
    Nope You smell the powerful odeur of Object Pascal. As far as I know, even C++ supports namespacing for classes - which is only an incomplete implementation of real "with"-namespacing. It may be true for VisualBASIC, but imho C++ is in no way any more powerful than Delphi. But better let's not start a flame war about languages.


    Anyway, thanks for your replies. At least now I don't need to search anymore.

  8. #8
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    Then I wonder what you mean by "quickly". Easier on the eye in syntax? "with" doesn't make it faster. It just makes it so that you don't have to type or see it. Yes, that would be the case in object pascal. It all compiles to something that has to go to an offset from an address. There's no speed difference in using something like "with".
    Last edited by FillYourBrain; 07-29-2003 at 08:52 AM.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  9. #9
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    plus, what you're calling overhead in C++ is not overhead. It's something that your design has caused to look a little uglier. If the method was on the class/struct itself you wouldn't need to deal with that.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  10. #10
    *******argv[] - hu? darksaidin's Avatar
    Join Date
    Jul 2003
    Posts
    314
    Originally posted by FillYourBrain
    Then I wonder what you mean by "quickly". Easier on the eye in syntax? "with" doesn't make it faster. It just makes it so that you don't have to type or see it. Yes, that would be the same in object pascal. It all compiles to something that has to go to an offset from an address. There's no speed difference in using something like "with".
    Yes, it compiles to the same code and it's neither faster nor slower. I can't remember having said anything about speed, but if I did I was probably referring to the time it takes to write 1x with somestruct {...} or 12 times somestruct. .

    I guess I should have simply said that I need to call an API function that takes a struct-parameter with a lot of members. Everybody would have understood my problem with initializing all those members in the first place then

    Originally posted by FillYourBrain
    plus, what you're calling overhead in C++ is not overhead. It's something that your design has caused to look a little uglier. If the method was on the class/struct itself you wouldn't need to deal with that.
    I have no influence on Microsofts Windows design.

  11. #11
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    >>Nope You smell the powerful odeur of Object Pascal. As far as I know, even C++ supports namespacing for classes - which is only an incomplete implementation of real "with"-namespacing. It may be true for VisualBASIC, but imho C++ is in no way any more powerful than Delphi. But better let's not start a flame war about languages.

    If the "with" syntax is the same in Object Pascal as it is in VB, then it doesnt have much to do with C++ namespaces. Namespaces in C++ are a compile time method of avoiding name clashes.....the "with" statement in vb;

    Code:
        Dim obj As Excel.Workbook
        
        With obj
            .Activate
            .NewWindow
        End With
    is a way of telling the compiler that all statments in the block are member calls to 1 object.....it's supposed to be quicker as the compiler will keep the one reference and apply all calls to it (so far as I know - I'm not a big VB coder)

  12. #12
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    I can't remember having said anything about speed, but if I did I was probably referring to the time it takes to write 1x with somestruct {...} or 12 times somestruct.
    you said "quickly access members". That's at the least is ambiguous.
    I have no influence on Microsofts Windows design.
    ???? I was talking about your design. Your twisted desire to group variables into structs and then not treat them as objects.

    tip for you: You should stop being so confrontational. You'll get along with people better.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  13. #13
    *******argv[] - hu? darksaidin's Avatar
    Join Date
    Jul 2003
    Posts
    314
    Originally posted by FillYourBrain
    you said "quickly access members". That's at the least is ambiguous. ???? I was talking about your design. Your twisted desire to group variables into structs and then not treat them as objects.
    English isn't my native language. Seriously, I did not intend quickly to mean that it compiles or runs faster.

    Originally posted by FillYourBrain
    tip for you: You should stop being so confrontational. You'll get along with people better.
    Sorry fib, but I thought you were being confrontal. I guess this is a case of misunderstanding, maybe based on my English.

    Can some moderator please lock this ? It will only end up with a flamefest and I already got my reply.

  14. #14
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    I'm not taking offense, its just that snapping back at people who are trying to help isn't going to get your questions answered any faster. You gave this snap-back response to newbie_grg
    Sorry, but I think my question is 100% clear (someone else correct me if I'm wrong ). The examples are not even needed to understand it, I just added them to make it easier to understand. "Is there a way to quickly access members of a struct without naming the struct every single time?" is accurate unless, which is possible, my grammar is somehow wrong.
    100% clear? how can you be sure if your english isn't so good? Point being. Snapping back at people is confrontational.

    I think the fact that you're coming at C++ from a different perspective (object pascal coding history) is why you are seeing things differently. C/C++ is a bit more difficult than most other languages. But in terms of speed/power/flexibility, you'll be hard pressed to find a match.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  15. #15
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Originally posted by darksaidin
    Can some moderator please lock this ? It will only end up with a flamefest and I already got my reply.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. access to devices
    By pastitprogram in forum C++ Programming
    Replies: 1
    Last Post: 06-06-2008, 11:16 PM
  2. ww hosting and SSH access
    By spoon_ in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 04-07-2005, 08:49 AM
  3. Replies: 3
    Last Post: 09-22-2003, 09:48 PM
  4. Direct disk access in DOS
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 02-26-2002, 02:52 PM