support bazel complie this project and format code.

This commit is contained in:
zhangxing
2023-03-30 00:15:11 +08:00
committed by light-city
parent 1f86192576
commit 7529ae3a55
636 changed files with 10025 additions and 9387 deletions

View 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.

View File

@@ -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;
}