Board index » cppbuilder » Cannot initialize a TFontStyles data member?

Cannot initialize a TFontStyles data member?

Thanks Remy, I began to suspect as much.  While I was waiting for your
response, I came up with the following solution (which almost exactly
matches your suggestion...great minds?):

 typedef struct
{
    int some_int;
    int StyleBits;

Quote
} MyStruct;

static MyStruct structs[] =
{
    { 1, 0x1 << fsBold | 0x1 << fsItalic},  // I need the ability to have
more than one style...
    { 2, 0x1 << fsBold },
    etc...

Quote
};

Then, to use the "StyleBits":

{
  TFontStyles style;  // start with an empty style set

  for ( int s=fsBold; s<=fsStrikeOut; s++ )
  {
    if ( (structs[some_index].StyleBits >> s) & 0x1 )
    {
      style = style << (TFontStyle)s;  // insert the current style
    }
  }
  DestinationStyle = style;  // assign the resulting style to the desired
destination

Quote
}

- Dennis

"Remy Lebeau (TeamB)" <gambi...@yahoo.com> wrote in message
news:3ddabecb$1@newsgroups.borland.com...

Quote
> In short - you can't.  What the technicalities behind it are, I don't
know.
> Maybe has something to do with TFontStyles being a templated class.  All I
> do know is that it doesn't work with any version of Builder that I tried
it
> with (3 through 6).

> If the style is ever only 1 value at a time, you could change the style to
> be a TFontStyle instead of a TFontStyles:

>     typedef struct
>     {
>         int some_int;
>         TFontStyle style;
>     } MyStruct;

>     static MyStruct structs[] =
>     {
>         { 1, fsBold },
>         { 2, fsItalic },
>         etc...
>     };

> Otherwise, you'll probably just have to initialize the struct manually one
> entry at a time, ie:

>     typedef struct
>     {
>         int some_int;
>         TFontStyle style;
>     } MyStruct;

>     static MyStruct structs[2];
>     static bool first = true;

>     if( first )
>     {
>         first = false;

>         structs[0].some_int = 1;
>         structs[0].style << fsBold;

>         structs[1].some_int = 2;
>         structs[1].style << fsItalic;

>         etc...
>     }

> Otherwise, you may have to set up a regular bitmask field instead of using
> TFontStyles at all:

>     #define BOLD 1
>     #define ITALIC 2
>     #define UNDERLINE 4

>     struct MyStruct
>     {
>         int some_int;
>         DWORD style;
>     };

>     static MyStruct structs[] = {
>         { 1, BOLD },
>         { 2, ITALIC },
>         { 3, ITALIC | UNDERLINE }
>         etc.
>     };

> Gambit

 

Re:Cannot initialize a TFontStyles data member?


In short - you can't.  What the technicalities behind it are, I don't know.
Maybe has something to do with TFontStyles being a templated class.  All I
do know is that it doesn't work with any version of Builder that I tried it
with (3 through 6).

If the style is ever only 1 value at a time, you could change the style to
be a TFontStyle instead of a TFontStyles:

    typedef struct
    {
        int some_int;
        TFontStyle style;
    } MyStruct;

    static MyStruct structs[] =
    {
        { 1, fsBold },
        { 2, fsItalic },
        etc...
    };

Otherwise, you'll probably just have to initialize the struct manually one
entry at a time, ie:

    typedef struct
    {
        int some_int;
        TFontStyle style;
    } MyStruct;

    static MyStruct structs[2];
    static bool first = true;

    if( first )
    {
        first = false;

        structs[0].some_int = 1;
        structs[0].style << fsBold;

        structs[1].some_int = 2;
        structs[1].style << fsItalic;

        etc...
    }

Otherwise, you may have to set up a regular bitmask field instead of using
TFontStyles at all:

    #define BOLD 1
    #define ITALIC 2
    #define UNDERLINE 4

    struct MyStruct
    {
        int some_int;
        DWORD style;
    };

    static MyStruct structs[] = {
        { 1, BOLD },
        { 2, ITALIC },
        { 3, ITALIC | UNDERLINE }
        etc.
    };

Gambit

Quote
"Dennis Jones" <djo...@nospam.com> wrote in message

news:3ddab9d1@newsgroups.borland.com...
Quote
> If I take out the TFontStyles data member and change
> the initialization accordingly, it compiles.  So, how can
> I initialize a TFontStyles data member in a structure
> like this?

Re:Cannot initialize a TFontStyles data member?


Hi,

I have a structure to which I want to add a TFontStyles data member.  Then I
want to initialize an array of these structures.  Here is some example code
that shows what I am trying to do:

typedef struct
{
  int some_int;
  TFontStyles style;

Quote
} MyStruct;

static MyStruct structs[] =
{
  { 1, TFontStyles() << fsBold },
  { 2, TFontStyles() << fsItalic },
  etc...

Quote
};

But in doing this, I get a bunch of compiler errors that make no sense:

"Cannot convert 'int' to 'MyStruct'",
"} expected"
"Declaration syntax error"
"Unexpected }"
"Unexpected }"
"Declaration terminated incorrectly"

If I take out the TFontStyles data member and change the initialization
accordingly, it compiles.  So, how can I initialize a TFontStyles data
member in a structure like this?

- Dennis

Other Threads