help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: struct member initialization


From: fernspaanakker
Subject: Re: struct member initialization
Date: 3 Jan 2007 08:27:32 -0800
User-agent: G2/1.0

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)
{
}



reply via email to

[Prev in Thread] Current Thread [Next in Thread]