-1

Trying to create empty constructor for a struct that has union as a member variable. Union also has a struct and an additional variable as member variables.

Took few approaches, but non were successful. How can I create an empty constructor for nested anonymous structures? How can I call the member variables afterwards?

typedef struct test_struct
{
   int a;
   union {
      int b;
      struct {
         int c;  
      };  
    };
 } test_struct; 

Below won't compile

 test_struct() : a(10), b(20), c(30) {
 }   

Below sets both b and c as 30

test_struct() : a(10), b(20) {
   c = 30;
 }  

Here is the print function

int main(void)
{  
   test_struct *ts = new test_struct();
   printf("%i\n%i\n%i\n", ts->a, ts->b, ts->c);
   return 0;
}

I trying to create an empty constructor that will print 10 20 30 with my printf().

EDIT: I received comments on how I cannot initialize union in this way. However, the header file I received (which I cannot change at all) has structs defined in such nested structure. I do not know how the initialization goes in the back, but my code has to print out values for all the member variables. (a, b, and c for the below example code). Any approaches/reading I can do to achieve this?

timrau
  • 21,494
  • 4
  • 47
  • 62
Mikan
  • 79
  • 6
  • 1
    Do you understand why it doesn't make sense to initialize both `b` and `c`? – Sneftel Jun 24 '19 at 17:42
  • 2
    https://stackoverflow.com/questions/4003087/whats-the-major-difference-between-union-and-struct-in-c – Mat Jun 24 '19 at 17:43
  • 3
    How do you expect to contain that union having two different values contained at the same time? You probably didn't understand what the `union` feature actually does. – πάντα ῥεῖ Jun 24 '19 at 17:43
  • Sidenote: C++ doesn't allow anonymous `struct`s. You are likely using a compiler that allows them by extension, but know your portability will be limited. – user4581301 Jun 24 '19 at 17:49
  • Recommended reading: [Why should C++ programmers minimize use of 'new'?](https://stackoverflow.com/questions/6500313/why-should-c-programmers-minimize-use-of-new) – user4581301 Jun 24 '19 at 17:53
  • You have probably received a `C` header file, not a `C++` header file. – Ted Lyngmo Jun 24 '19 at 18:08
  • @TedLyngmo I think that might be the case. However, does that make any different in functionality of the code? – Mikan Jun 24 '19 at 18:13
  • @Anaguone Yes, you will not be able to add constructors etc. A struct/union combo like the above is leagal in C (although you have at least one syntax error and a typedef that isn't typedef:ing) but it's not in C++. – Ted Lyngmo Jun 24 '19 at 18:15
  • @TedLyngmo I made one fix on the above code to match exactly with the file I am look at. Are there still a syntax error in this? – Mikan Jun 24 '19 at 18:24
  • @Anaguone No, now the syntax and typedef is ok for `C`. The anonymous struct makes it invalid `C++`. – Ted Lyngmo Jun 24 '19 at 18:32
  • @TedLyngmo I see thank you – Mikan Jun 24 '19 at 18:35

1 Answers1

0

Apart from the erroneous way you use union, let's stick to the question about your nested struct. The answer is simple: you can't specify constructors for nested structs. So, even if you fix the code and use correctly your nested struct, you won't be able to declare a constructor or destructor for that anonymous class/struct because the constructor and destructor names need to match the class name. In the example you posted, the anonymous class is local. It means that it has no linkage so mangled name is not even created.

Alex Gidan
  • 2,332
  • 14
  • 26
  • Hello, thank you for the reply. However, the header file I received (which I cannot change at all) has structs defined in such nested fashion. I do not know how the initialization goes in the back, but my code has to print out values for all the member variables. (a, b, and c for the above example code). Any approaches/reading I can do to achieve this? – Mikan Jun 24 '19 at 17:57
  • @Anaguone If you can't change the header file, how come you are adding constructors to it? – Ted Lyngmo Jun 24 '19 at 18:03
  • I was trying to create my own struct in the same way, initialize it with empty constructor, then write a code that would read all the member variables. Does this structure signify that b and c has to have equal values at all times? – Mikan Jun 24 '19 at 18:04
  • @Anaguone You cannot. `union`s are one variable with multiple possible types all occupying the same space. Since `b` and `c` occupy the same space, setting `c` overwrites `b`. The behaviour of accessing `b` after setting `c` is undefined. – user4581301 Jun 24 '19 at 18:04