F
fabio
Hello,
zero-sized arrays (which I find very useful, since I do a lot of
packet-style programming with circular buffers) cannot be declared
if they're in a struct/class that later will be inherited (i.e. in
a base class), we know.
In ANSI C++ you can't actually instantiate such a structure, if I'm
not mistaken. Example:
struct MyBaseClass {
char * Name;
int Properties;
int Flags;
int Size;
unsigned char Data[0];
};
works as long as you then don't use it to create another struct/class,
for example:
class MyDerivedClass : MyBaseClass {
int AddedStuff;
};
The above generates an error, not a warning.
Which I find an annoying limitation. It would be perfectly justified
because while inheriting, it's not possible to know where to add the
new stuff if there's an unknown-size object before. But I want it as
a label, so it would be perfectly doable in the specific, [0] size
case (you see, I inherited my programming style from assembly). Of
course it would be impossible in a [] case (I'm talking
syntactically here, unfortunately for C/C++ [] and [0] seem to be
the same thing, but they shouldn't really).
Is there a fix or a serious workaround for this? Even if it's not
perfectly ANSI C++ compliant. BTW: putting [0] instead of [] as I
mentioned doesn't help. Neither the use of void (I had to try ).
Labels can't be declared there. What a disappointment.
Actually the way I work around it is annoying too:
I remove the "unsigned char Data[0];" part from the BaseClass, and
add it to the derived class. But since Data needs to be used by
members of BaseClass (that's what I want in the end, otherwise the
whole OOP of C++ becomes far from ideal for me), I access the Data
via a pointer to BaseClass+sizeof(BaseClass), eventually casted to
a pointer to the derivedclass when I want to pass it to a function
that accepts that type. Ugly, but as I said I like to think in asm-
like terms (to be more exact, in "memory, labels and types" terms).
Thanks for any hints xor suggestions, and have a nice day!
Fabio
zero-sized arrays (which I find very useful, since I do a lot of
packet-style programming with circular buffers) cannot be declared
if they're in a struct/class that later will be inherited (i.e. in
a base class), we know.
In ANSI C++ you can't actually instantiate such a structure, if I'm
not mistaken. Example:
struct MyBaseClass {
char * Name;
int Properties;
int Flags;
int Size;
unsigned char Data[0];
};
works as long as you then don't use it to create another struct/class,
for example:
class MyDerivedClass : MyBaseClass {
int AddedStuff;
};
The above generates an error, not a warning.
Which I find an annoying limitation. It would be perfectly justified
because while inheriting, it's not possible to know where to add the
new stuff if there's an unknown-size object before. But I want it as
a label, so it would be perfectly doable in the specific, [0] size
case (you see, I inherited my programming style from assembly). Of
course it would be impossible in a [] case (I'm talking
syntactically here, unfortunately for C/C++ [] and [0] seem to be
the same thing, but they shouldn't really).
Is there a fix or a serious workaround for this? Even if it's not
perfectly ANSI C++ compliant. BTW: putting [0] instead of [] as I
mentioned doesn't help. Neither the use of void (I had to try ).
Labels can't be declared there. What a disappointment.
Actually the way I work around it is annoying too:
I remove the "unsigned char Data[0];" part from the BaseClass, and
add it to the derived class. But since Data needs to be used by
members of BaseClass (that's what I want in the end, otherwise the
whole OOP of C++ becomes far from ideal for me), I access the Data
via a pointer to BaseClass+sizeof(BaseClass), eventually casted to
a pointer to the derivedclass when I want to pass it to a function
that accepts that type. Ugly, but as I said I like to think in asm-
like terms (to be more exact, in "memory, labels and types" terms).
Thanks for any hints xor suggestions, and have a nice day!
Fabio