why unions and structs couldnt be initialized ?

This is a discussion on why unions and structs couldnt be initialized ? within the C++ Programming forums, part of the General Programming Boards category; Howdy~ I just found when I want to initialize variables inside a union or struct compiler will report an error, ...

  1. #1
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563

    why unions and structs couldnt be initialized ?

    Howdy~

    I just found when I want to initialize variables inside a union or struct compiler will report an error, why couldnt we do that ???
    Never end on learning~

  2. #2
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Either declare the members static (remember, unions can't have static members) or, create an object of the particular struct/union and initilize those variables.

    Some code would help too.

  3. #3
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    Originally posted by Eibro
    Either declare the members static (remember, unions can't have static members) or, create an object of the particular struct/union and initilize those variables.

    Some code would help too.
    thanx. but I wonder why we couldnt initialize variable just as class ?
    Never end on learning~

  4. #4
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    You can, but maybe not in a constructor as you may be thinking.
    Post some code.

  5. #5
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    Originally posted by Eibro
    You can, but maybe not in a constructor as you may be thinking.
    Post some code.
    for example:

    Code:
    union myUnion
    {
      int a=0;
      bool b;
      char c;
    }
    if we run this code will get an error, why couldnt we do that ???
    Last edited by black; 11-11-2002 at 12:33 AM.
    Never end on learning~

  6. #6
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786
    You can specify a union constructor as you would with a struct or class....do so and set the member with the largest varible type to zero.

    Code:
    #include <iostream>
    
    
    union myUnion
    {
    	myUnion():a(0){}
    	int a;
    	bool b;
    	char c;
    };
    
    int main() {
    	
    	myUnion u;
    	
    	std::cout << "As int " << u.a << std::endl;
    	std::cout << "As bool " << u.b << std::endl;
    	std::cout << "As char " << u.c << std::endl;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unions & Structs... Puzzled.
    By Sparrowhawk in forum C Programming
    Replies: 10
    Last Post: 12-14-2008, 03:45 PM
  2. classes, structs and unions
    By Luciferek in forum C++ Programming
    Replies: 24
    Last Post: 08-09-2008, 10:26 AM
  3. unions and structs
    By Ron in forum C Programming
    Replies: 24
    Last Post: 07-24-2008, 10:54 PM
  4. Possible to create methods for data in structs?
    By eccles in forum C Programming
    Replies: 19
    Last Post: 12-15-2004, 08:46 AM
  5. Structs and Unions
    By Encrypted in forum C Programming
    Replies: 4
    Last Post: 11-05-2002, 02:53 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21