support bazel complie this project and format code.
This commit is contained in:
8
basic_content/union/BUILD
Normal file
8
basic_content/union/BUILD
Normal file
@@ -0,0 +1,8 @@
|
||||
# please run `bazel run basic_content/union:union`
|
||||
load("@rules_cc//cc:defs.bzl", "cc_binary")
|
||||
|
||||
cc_binary(
|
||||
name = "union",
|
||||
srcs = ["union.cpp"],
|
||||
copts = ["-std=c++11"]
|
||||
)
|
Binary file not shown.
@@ -6,45 +6,46 @@
|
||||
* @date 2019-08-06
|
||||
*/
|
||||
|
||||
#include<iostream>
|
||||
#include <iostream>
|
||||
/**
|
||||
* 默认访问控制符为public
|
||||
*/
|
||||
union UnionTest {
|
||||
/**
|
||||
* 可以含有构造函数、析构函数
|
||||
*/
|
||||
UnionTest() : i(10) {print(i);};
|
||||
~UnionTest(){};
|
||||
int i;
|
||||
/**
|
||||
* 可以含有构造函数、析构函数
|
||||
*/
|
||||
UnionTest() : i(10) { print(i); };
|
||||
~UnionTest(){};
|
||||
int i;
|
||||
|
||||
private:
|
||||
void print(int i){std::cout<<i<<std::endl;};
|
||||
void print(int i) { std::cout << i << std::endl; };
|
||||
};
|
||||
/**
|
||||
* 全局匿名联合必须是静态的
|
||||
* 全局匿名联合必须是静态的
|
||||
*/
|
||||
static union {
|
||||
int i;
|
||||
double d;
|
||||
int i;
|
||||
double d;
|
||||
};
|
||||
|
||||
int main() {
|
||||
UnionTest u;
|
||||
UnionTest u;
|
||||
|
||||
union {
|
||||
int i;
|
||||
double d;
|
||||
};
|
||||
union {
|
||||
int i;
|
||||
double d;
|
||||
};
|
||||
|
||||
std::cout << u.i << std::endl; // 输出 UnionTest 联合的 10
|
||||
std::cout << u.i << std::endl; // 输出 UnionTest 联合的 10
|
||||
|
||||
::i = 20;
|
||||
std::cout << ::i << std::endl; // 输出全局静态匿名联合的 20
|
||||
/**
|
||||
* 匿名union在定义所在作用域可直接访问union成员
|
||||
*/
|
||||
i = 30;
|
||||
std::cout << i << std::endl; // 输出局部匿名联合的 30
|
||||
::i = 20;
|
||||
std::cout << ::i << std::endl; // 输出全局静态匿名联合的 20
|
||||
/**
|
||||
* 匿名union在定义所在作用域可直接访问union成员
|
||||
*/
|
||||
i = 30;
|
||||
std::cout << i << std::endl; // 输出局部匿名联合的 30
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
Reference in New Issue
Block a user