回顾struct,union的申明,初始化,io处理

/* 
 * File:   main.cpp
 * Author: Vicky.H
 * Email:  eclipser@163.com
 */
#include <iostream>
#include <typeinfo>
#include <string.h>

void f(void) {
    struct A {
        short age;
        int money;
        char name[12];
    }; // 12 + 2 + 4 = 18  但实际上是20,因为 结构体的长度以 4 * n
    std::cout << sizeof(struct A) << "\t" << typeid(struct A).name() << std::endl;
    union B {
        short age;
        int money;
        char name[12];
    }; // 公用体的长度以 4 * n
    
    std::cout << sizeof(union B) << "\t" << typeid(union B).name() << std::endl;
}

/*
 * 
 */
int main(void) {
    
    f();
    f();
    f();
    
    struct A {};
    std::cout << typeid(struct A).name() << std::endl;
    
    std::cout << "---------------------------" << std::endl;
    
    union B {
        short age;
        int money;
        char name[12];
    };

    B b;
    b.age = 2;
    std::cout << b.age << std::endl;
    b.money = 999;
    std::cout << b.age << "\t" << b.money << std::endl;
    strcpy(b.name,"Jack");
    std::cout << b.age << "\t" << b.money << "\t" << b.name << std::endl;
    
    
    return 0;
}

执行:

20      Z1fvE1A
12      Z1fvE1B
20      Z1fvE1A
12      Z1fvE1B
20      Z1fvE1A
12      Z1fvE1B
Z4mainE1A
---------------------------
2
999     999
24906   1801675082      Jack

 

理解编译期与运行期:
这里主要讨论的是struct 与 union 申明位置.这个实例中,同事声明了2个struct A 以及 2个union B,分别在main()函数之外的函数体内申明以及main()函数之内申明.

需要注意的是,即便是在函数体中申明的结构体,也是在编译期间就定义好的,也就是说,是静态生成的,并非程序执行到申明结构体的代码才动态生成的!这和全局变量,局部变量,全局函数,局部函数,以及全局#include,局部#include,全局using name namespace xxx.局部using namespace xxx是一样的,都是静态编译期间就完成的定义,并非动态时定义!不同之处就是限制了使用的范围而已.本质上没有任何区别,不会导致更多的性能消耗,这里我们使用typeid来证明了并非程序执行到申明结构体的行才动态申明结构体,因为,他们typeid是相同的!!!

 

 

共用体,IO处理:

/* 
 * File:   main.cpp
 * Author: Vicky.H
 * Email:  eclipser@163.com
 */
#include <iostream>
#include <fstream>   
#include <sstream>   
#include <cstring> 

struct Animal 
{

    int kind;
    
    union {
        char human[10];
        char bird[20];
        char fish[30];
    };
    
    char* getName(void) {
        if (kind == 1) {
            return human;
        } else if (kind == 2) {
            return bird;
        } else {
            return fish;
        }
    }
    
    // 考虑IO处理
    friend std::ostream& operator<<(std::ostream& os, const Animal& ref);  
    friend std::istream& operator>>(std::istream& is, Animal& ref);  

};

std::ostream& operator<<(std::ostream& os, const Animal& ref) {  
    os << ref.kind << '\n' /**需要换行符*/;
//    if (ref.kind == 1) {
//        os << ref.human;
//    } else if (ref.kind == 2) {
//        os << ref.bird;
//    } else {
//        os << ref.fish;
//    }
    os << ref.fish;
    return os;  
}  
  
std::istream& operator>>(std::istream& is, Animal& ref) {  
    is >> ref.kind >> ref.fish;  
    return is;  
}  



/*
 * 
 */
int main(void) {
    Animal human = {1,"Jack"};
    std::cout << human.getName() << std::endl;
    
    std::ofstream ofs("./tmp.txt", std::ios::trunc | std::ios::binary);  
    ofs << human; // 将对象输入到文件中   
    ofs.flush();  
    ofs.close();
    
    std::ifstream ifs("./tmp.txt", std::ios::in | std::ios::binary);  
    Animal a1;  
    ifs >> a1; //  读取文件内容,将其转换为对象   
    ifs.close();  
    std::cout << a1 << std::endl;  
    
    std::cout << "---------------------------" << std::endl;
    
    Animal bird = {2,"Sparrow"};
    std::cout << bird.getName() << std::endl;
    
    std::ofstream ofs2("./tmp2.txt", std::ios::trunc | std::ios::binary);  
    ofs2 << bird; // 将对象输入到文件中   
    ofs2.flush();  
    ofs2.close();
    
    std::ifstream ifs2("./tmp2.txt", std::ios::in | std::ios::binary);  
    Animal a2;  
    ifs2 >> a2; //  读取文件内容,将其转换为对象   
    ifs2.close();  
    std::cout << a2 << std::endl;  
    
    std::cout << "---------------------------" << std::endl;
    
    Animal fish = {3, "Dolphin"};
    std::cout << fish.getName() << std::endl;
    
    std::ofstream ofs3("./tmp3.txt", std::ios::trunc | std::ios::binary);  
    ofs3 << fish; // 将对象输入到文件中   
    ofs3.flush();  
    ofs3.close();
    
    std::ifstream ifs3("./tmp3.txt", std::ios::in | std::ios::binary);  
    Animal a3;  
    ifs3 >> a3; //  读取文件内容,将其转换为对象   
    ifs3.close();  
    std::cout << a3 << std::endl;  
    
    


    
    return 0;
}

 

运行:
Jack
1
Jack
---------------------------
Sparrow
2
Sparrow
---------------------------
Dolphin
3
Dolphin

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值