Tuesday, February 23, 2016

[C++] struct union enum

struct

struct student 
{
    char name[6]; 
    int age; 
    char* GetName(void){return name;};
    int GetAge(void){return age;};
};
 struct VS class : default in struct is public, default class is private

union

Union -> share the memory for all the members : when you create an union, compiler will give a space of the max size of all the members. and when you use on member and give a number, the value will be covered.

union score 
{
    int i_sc; 
    float f_sc;
    int GetInt(void){return i_sc;};
    float GetFloat(void){return f_sc;};
};

union VS class : 
1 Non inherit in union
2 Non virtual method
3 Default public in union  
4 Non static, non reference -> (because union cannot share the memory)
5 the class with constructor,copy constructor,destructor,copy assignment operator(拷贝赋值运算符), virtual function cannot be a member of union : this class shared the memory.

enum

enum color {red,blak,white,blue,yellow};



No comments:

Post a Comment