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

30
basic_content/using/BUILD Normal file
View File

@@ -0,0 +1,30 @@
# please run `bazel run basic_content/using:derived_base`
# please run `bazel run basic_content/using:using_derived`
# please run `bazel run basic_content/using:using_global`
# please run `bazel run basic_content/using:using_typedef`
load("@rules_cc//cc:defs.bzl", "cc_binary")
# Don't panic if you get compilation errors, this is what this code demonstrates, as expected.
cc_binary(
name = "derived_base",
srcs = ["derived_base.cpp"],
copts = ["-std=c++11"]
)
cc_binary(
name = "using_derived",
srcs = ["using_derived.cpp"],
copts = ["-std=c++11"]
)
cc_binary(
name = "using_global",
srcs = ["using_global.cpp"],
copts = ["-std=c++11"]
)
cc_binary(
name = "using_typedef",
srcs = ["using_typedef.cpp"],
copts = ["-std=c++11"]
)

Binary file not shown.

View File

@@ -3,26 +3,28 @@
using namespace std;
class Base1 {
public:
Base1():value(10) {}
virtual ~Base1() {}
void test1() { cout << "Base test1..." << endl; }
protected: // 保护
int value;
public:
Base1() : value(10) {}
virtual ~Base1() {}
void test1() { cout << "Base test1..." << endl; }
protected: // 保护
int value;
};
// 默认为私有继承
class Derived1 : Base1 {
public:
void test2() { cout << "value is " << value << endl; }
public:
void test2() { cout << "value is " << value << endl; }
};
class Base {
public:
Base():value(20) {}
virtual ~Base() {}
void test1() { cout << "Base test1..." << endl; }
private: //私有
int value;
public:
Base() : value(20) {}
virtual ~Base() {}
void test1() { cout << "Base test1..." << endl; }
private: // 私有
int value;
};
/**
@@ -31,18 +33,16 @@ class Base {
*
*/
class Derived : Base {
public:
using Base::value;
void test2() { cout << "value is " << value << endl; }
public:
using Base::value;
void test2() { cout << "value is " << value << endl; }
};
int main() {
Derived1 d1;
d1.test2();
int main()
{
Derived1 d1;
d1.test2();
Derived d;
d.test2();
return 0;
Derived d;
d.test2();
return 0;
}

Binary file not shown.

View File

@@ -9,28 +9,21 @@
#include <iostream>
using namespace std;
class Base{
public:
void f(){ cout<<"f()"<<endl;
}
void f(int n){
cout<<"Base::f(int)"<<endl;
}
class Base {
public:
void f() { cout << "f()" << endl; }
void f(int n) { cout << "Base::f(int)" << endl; }
};
class Derived : private Base {
public:
using Base::f;
void f(int n){
cout<<"Derived::f(int)"<<endl;
}
public:
using Base::f;
void f(int n) { cout << "Derived::f(int)" << endl; }
};
int main()
{
Base b;
Derived d;
d.f();
d.f(1);
return 0;
int main() {
Derived d;
d.f();
d.f(1);
return 0;
}

Binary file not shown.

View File

@@ -8,38 +8,28 @@
#include <iostream>
#define isNs1 1
//#define isGlobal 2
// #define isGlobal 2
using namespace std;
void func()
{
cout<<"::func"<<endl;
}
void func() { cout << "::func" << endl; }
namespace ns1 {
void func()
{
cout<<"ns1::func"<<endl;
}
}
void func() { cout << "ns1::func" << endl; }
} // namespace ns1
namespace ns2 {
#ifdef isNs1
using ns1::func; /// ns1中的函数
#ifdef isNs1
using ns1::func; /// ns1中的函数
#elif isGlobal
using ::func; /// 全局中的函数
using ::func; /// 全局中的函数
#else
void func()
{
cout<<"other::func"<<endl;
}
void func() { cout << "other::func" << endl; }
#endif
}
} // namespace ns2
int main()
{
/**
* 这就是为什么在c++中使用了cmath而不是math.h头文件
*/
ns2::func(); // 会根据当前环境定义宏的不同来调用不同命名空间下的func()函数
return 0;
int main() {
/**
* 这就是为什么在c++中使用了cmath而不是math.h头文件
*/
ns2::func(); // 会根据当前环境定义宏的不同来调用不同命名空间下的func()函数
return 0;
}

Binary file not shown.

View File

@@ -1,6 +1,6 @@
/**
* @file using_typedef.cpp
* @brief g++ -o using_typedef using_typedef.cpp -std=c++11
* @brief bazel run basic_content/using:using_typedef
* 取代typedef使用using来定义别名
* @author 光城
* @version v1
@@ -14,21 +14,19 @@ using namespace std;
typedef vector<int> V1;
using V2 = vector<int>;
int main() {
int nums1[] = {1, 2, 3, 4, 5, 6};
V1 vec1(nums1, nums1 + sizeof(nums1) / sizeof(int));
int nums2[] = {5, 7, 6};
V2 vec2(nums2, nums2 + sizeof(nums2) / sizeof(int));
int main()
{
int nums1[] = {1,2,3,4,5,6};
V1 vec1(nums1,nums1+sizeof(nums1)/sizeof(int));
int nums2[] = {5,7,6};
V2 vec2(nums2,nums2+sizeof(nums2)/sizeof(int));
for(auto i:vec1)
cout<<i<<" ";
cout<<endl;
for (auto i : vec1)
cout << i << " ";
cout << endl;
for(auto i:vec2)
cout<<i<<" ";
cout<<endl;
for (auto i : vec2)
cout << i << " ";
cout << endl;
return 0;
return 0;
}