Thread: Typedefining members of structs

  1. #16
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>In other words, everytime you change a member with the dot operator, don't you have to make an assignment for it to be reflected in the system structure?

    I was assuming that it's a structure provided for your own convenience, and also occasionally passed to a system function. In the convenience part, you simply don't use a RectangleType at all - you just work with the MyRectangleType until you need to pass it to a system function, at which point the conversion operator gives you some extent of compatibility. If the system function takes a RectangleType reference as an 'out' parameter though, this obviously won't work. You'd need to then pass an actual RectangleType and then convert to MyRectangleType via some conversion constructor and/or assignment operator.

    Depending on the ratio of such system calls vs. just working with the structure itself, the MyRectangleType solution has the potential to be either really useful or really annoying

    Another idea I just thought up (though I haven't tested it at all, so I don't know if it'll work):
    Code:
    struct MyRectangleType
    {
        RectangleType rt;
    
        int X;
        int Y;
        int W;
        int H;
    
        MyRectangleType(const RectangleType& orig)
        :rt(orig)
        {
            fromRT();
        }
        operator = (const RectangleType& rhs)
        {
            rt = rhs;
            fromRT();
        }
        operator RectangleType&()
        {
            rt.topLeft.x = X;
            rt.topLeft.y = Y;
            rt.extent.x = W;
            rt.extent.y = H;
            return rt;
        }
        void fromRT()
        {
            X = rt.topLeft.x;
            Y = rt.topLeft.y;
            W = rt.extent.x;
            H = rt.extent.y;
        }
    };
    
    ...
    
    MyRectangleType mrt;
    mrt.X = 5;
    mrt.Y = 7;
    mrt.W = 20;
    mrt.H = 30;
    
    someSystemCall(mrt);  //where parameter is IN and by reference/value
    someOtherCall(mrt);  //where parameter is OUT and by reference
    
    mrt.fromRT();
    cout << mrt.W << mrt.H << mrt.X << mrt.Y << endl;
    
    mrt = someOtherOtherCall();
    cout << mrt.W << mrt.H << mrt.X << mrt.Y << endl;
    **EDIT**
    No matter what, any solution such as this that heavily involves type conversions will be extremely inefficient compared to just using the original structure or a wrapper around it, as other have suggested. While this may not matter in some cases, in cases where many such conversions are required in a short period of time, there will probably be a noticeable slowdown especially on a Palm. Also, the memory required for the structure will be at least doubled if you use this particular class, which *could* become a problem depending on how much is available at any given time.
    Last edited by Hunter2; 04-03-2005 at 01:52 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  2. #17
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    you just work with the MyRectangleType until you need to pass it to a system function, at which point the conversion operator gives you some extent of compatibility.
    Ahhh...I see: an automatic conversion would occur if you passed a MyRectangleType to a function needing a RectangleType.

    If the system function takes a RectangleType reference as an 'out' parameter though,
    What's an 'out' parameter? A reference that can be used to stuff in some ouput from the function? In any case, it looks like the op anticipated that problem:
    The operator RectangleType() method wouldn't work because what happens when I pass it by reference? That's taking the address of a temporary.
    Last edited by 7stud; 04-03-2005 at 01:57 PM.

  3. #18
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>What's an 'out' parameter?
    'out' for output. As I meant it, it's a parameter passed by reference to a function as a mechanism by which the function can send values back to the caller. Mostly I see it used in the documentation of function prototypes (might have been MSDN though I can't remember), in the form of a comment beside a specific parameter, saying "OUT" or "IN". As such, I'm not sure if my grammatical usage here is correct, but I'm sticking with it 'till the bitter end
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  4. #19
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    In any case, it looks like the op anticipated that problem:
    Quote:
    The operator RectangleType() method wouldn't work because what happens when I pass it by reference? That's taking the address of a temporary.
    I saw that, but couldn't find it when I was coming back to take the quote. In any case, this is not always a problem - take for example:
    Code:
    void abcd(const std::string& x)
    {}
    
    ...
    abcd(std::string("cdef"));
    I have seen and done this numerous times in the past, and as far as I know it is perfectly valid - the temporary stays in scope for the duration of the function, so the 'pointer' is valid until the function returns. I ran a quick test with a non-const reference and it compiled fine as well, so I assume it works in that case too. The problem, as I have mentioned [edit] and I see you have too [/edit], is with by-reference parameters that are intended to be checked later by the caller: Since the temp goes out of scope when the function returns, the result will be lost.

    As such, I have attempted to add support for this in my second version of MyRectangleType by keeping a member RectangleType and passing it where a RectangleType reference is required.
    Last edited by Hunter2; 04-03-2005 at 02:10 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dynamicly adding data members to structs
    By ITAmember in forum C Programming
    Replies: 11
    Last Post: 06-01-2009, 03:26 PM
  2. Structs, members, and casting
    By Nick Howes in forum C Programming
    Replies: 4
    Last Post: 03-05-2008, 12:54 PM
  3. alignment (classes, structs, members)
    By Raven Arkadon in forum C++ Programming
    Replies: 5
    Last Post: 04-07-2006, 06:51 AM
  4. accessing members in nested structs
    By amidstTheAshes in forum C Programming
    Replies: 2
    Last Post: 03-23-2005, 02:00 PM
  5. Passing members of structs to functions
    By CV3 in forum C Programming
    Replies: 6
    Last Post: 09-23-2004, 01:56 PM