Hi,

Sorry if my example code was incomplete. The code is actually from a
string class which was written in MSVC++. The string class has an
optimization to store small strings locally in the class without using
dynamic memory allocation for "string". If "this->string" has a valid
pointer, the union uses strLen, otherwise the union contains both the
string and the length (localStrLen).
I tried to name the struct and use the initialization like you
described in the previous reply, but g++ complains that you cannot use
such an initialization in a union. Should I just remove the union and
accept the extra 4 bytes for the strLen or can the compiler warnings be
fixed somehow?

class nString
{
public:
    /// constructor
    nString();
protected:
    enum
    {
        LOCALSTRINGSIZE = 14,
    };
    char* string;
    union
    {
        struct
        {
            char localString[LOCALSTRINGSIZE];
            ushort localStrLen;
        };
        uint strLen;
    };
};
//------------------------------------------------------------------------------
/**
*/
inline
nString::nString() :
    string(0),
    strLen(0),
    localStrLen(0)
{
}

_______________________________________________
help-gplusplus mailing list
help-gplusplus@gnu.org
http://lists.gnu.org/mailman/listinfo/help-gplusplus

Reply via email to