c++ - Variable class/struct structure? (Not template & not union?) -


i have tried union...

struct foo {     union     {         struct // 2 bytes         {             char var0_1;         };         struct // 5 bytes         {             char var1_1;             int var1_2;         };     }; }; 

problem: unions want, except take size of biggest datatype. in case need struct foo have initialization allows me tell structure chose of 2 (if legal) shown below.

so after that, tried class template overloading...

template <bool b> class foo { }  template <> class foo<true> {     char var1; }  template <> class foo<false> {     char var0;     int var1; } 

problem: happy templates , fact use same variable name on char , int, problem syntax. because classes created on compile-time, template boolean variable needed hardcoded constant, in case boolean needs user-defined on runtime.

so need of 2 "worlds." how can achieve i'm trying do?

!!note: foo class/struct later inherited, therefore mentioned, size of foo of utmost importance.

edit#1:: application:

basically used read/write (using pointer interface) specific data buffer , allow me create (new instance of class/struct) same data buffer. variables see above specify length. if it's smaller data buffer, length written in char/byte. if it's bigger data buffer, first char/byte null flag, , int specifies length instead. after length it's obvious actual data follows, hence why inheritance. size of class of utmost importance. need have cake , eat too.

your solution not make sense. think solution: define 2 independents classes: footrue , foofalse corresponding members same result.

probably, looking different solution inheritance. example, footrue basefoo , foofalse derivedfoo previous 1 base , extends int member.

in case, have polymorphism method work in runtime.


Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -