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

17
basic_content/const/BUILD Normal file
View File

@@ -0,0 +1,17 @@
# please run `bazel run basic_content/const:const_function`
# please run `bazel run basic_content/const:const_num`
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 = "const_function",
srcs = ["const_function.cpp"],
copts = ["-std=c++11"]
)
# Don't panic if you get compilation errors, this is what this code demonstrates, as expected.
cc_binary(
name = "const_num",
srcs = ["const_num.cpp"],
copts = ["-std=c++11"]
)

View File

@@ -298,41 +298,42 @@ public:
int getCount() const;
};
//main.cpp
#include<iostream>
#include"apple.cpp"
using namespace std;
Apple::Apple(int i):apple_number(i)
// apple.cpp
Apple::Apple(int i) : apple_number(i)
{
}
int Apple::add(){
take(1);
int Apple::add(int num)
{
take(num);
return 0;
}
int Apple::add(int num) const{
int Apple::add(int num) const
{
take(num);
return num;
return 0;
}
void Apple::take(int num) const
{
cout<<"take func "<<num<<endl;
std::cout << "take func " << num << std::endl;
}
int Apple::getCount() const
{
take(1);
add(); // error
// add(); //error
return apple_number;
}
int main(){
int main()
{
Apple a(2);
cout<<a.getCount()<<endl;
cout << a.getCount() << endl;
a.add(10);
const Apple b(3);
b.add(100);
return 0;
}
// main.cpp
```
> 编译: g++ -o main main.cpp apple.cpp<br>
> 编译:bazel run basic_content/const/class_const/first_example:main<br>
此时报错上面getCount()方法中调用了一个add方法而add方法并非const修饰所以运行报错。也就是说const成员函数只能访问const成员函数。

View File

@@ -0,0 +1,16 @@
# please run `bazel run basic_content/const/class_const/c++11_example:main`
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library")
cc_library(
name = "apple",
srcs = ["apple.cpp"],
hdrs = ["apple.h"],
)
cc_binary(
name = "main",
srcs = ["main.cpp"],
deps = [
":apple",
],
)

View File

@@ -1,15 +1,19 @@
class Apple
{
private:
int people[100];
public:
Apple(int i);
//使用c++11标准编译
static const int apple_number=10;
//const int apple_number=10;
void take(int num) const;
int add(int num);
int add(int num) const;
int getCount() const;
};
#include "apple.h"
#include <iostream>
using namespace std;
Apple::Apple(int i) {}
int Apple::add(int num) {
take(num);
return 0;
}
int Apple::add(int num) const {
take(num);
return 0;
}
void Apple::take(int num) const { cout << "take func " << num << endl; }
int Apple::getCount() const {
take(1);
// add(); //error
return apple_number;
}

View File

@@ -0,0 +1,13 @@
#pragma once
class Apple {
public:
Apple(int i);
// 使用c++11标准编译
static const int apple_number = 10;
// const int apple_number=10;
void take(int num) const;
int add(int num);
int add(int num) const;
int getCount() const;
};

View File

@@ -1,31 +1,11 @@
#include<iostream>
#include"apple.cpp"
#include "apple.h"
#include <iostream>
using namespace std;
Apple::Apple(int i)
{
}
int Apple::add(int num){
take(num);
}
int Apple::add(int num) const{
take(num);
}
void Apple::take(int num) const
{
cout<<"take func "<<num<<endl;
}
int Apple::getCount() const
{
take(1);
// add(); //error
return apple_number;
}
int main(){
Apple a(2);
cout<<a.getCount()<<endl;
a.add(10);
const Apple b(3);
b.add(100);
return 0;
int main() {
Apple a(2);
cout << a.getCount() << endl;
a.add(10);
const Apple b(3);
b.add(100);
return 0;
}

View File

@@ -0,0 +1,16 @@
# please run `bazel run basic_content/const/class_const/first_example:main`
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library")
cc_library(
name = "apple",
srcs = ["apple.cpp"],
hdrs = ["apple.h"],
)
cc_binary(
name = "main",
srcs = ["main.cpp"],
deps = [
":apple",
],
)

View File

@@ -1,13 +1,20 @@
class Apple
{
private:
int people[100];
public:
Apple(int i);
const int apple_number;
void take(int num) const;
int add(int num);
int add(int num) const;
int getCount() const;
};
#include "apple.h"
#include <iostream>
Apple::Apple(int i) : apple_number(i) {}
int Apple::add(int num) {
take(num);
return 0;
}
int Apple::add(int num) const {
take(num);
return 0;
}
void Apple::take(int num) const {
std::cout << "take func " << num << std::endl;
}
int Apple::getCount() const {
take(1);
// add(); //error
return apple_number;
}

View File

@@ -0,0 +1,11 @@
#pragma once
class Apple {
public:
Apple(int i);
const int apple_number;
void take(int num) const;
int add(int num);
int add(int num) const;
int getCount() const;
};

View File

@@ -1,32 +1,12 @@
#include<iostream>
#include"apple.cpp"
#include "apple.h"
#include <iostream>
using namespace std;
Apple::Apple(int i):apple_number(i)
{
}
int Apple::add(int num){
take(num);
}
int Apple::add(int num) const{
take(num);
}
void Apple::take(int num) const
{
cout<<"take func "<<num<<endl;
}
int Apple::getCount() const
{
take(1);
// add(); //error
return apple_number;
}
int main(){
Apple a(2);
cout<<a.getCount()<<endl;
a.add(10);
const Apple b(3);
b.add(100);
return 0;
int main() {
Apple a(2);
cout << a.getCount() << endl;
a.add(10);
const Apple b(3);
b.add(100);
return 0;
}

View File

@@ -0,0 +1,18 @@
# please run `bazel run basic_content/const/class_const/overload_example:main`
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library")
cc_library(
name = "apple",
srcs = ["apple.cpp"],
hdrs = ["apple.h"],
)
# Don't panic if you get compilation errors, this is what this code demonstrates, as expected.
cc_binary(
name = "main",
srcs = ["main.cpp"],
deps = [
":apple",
],
copts = ["-std=c++11"]
)

View File

@@ -1,13 +1,22 @@
class Apple
{
private:
int people[100];
public:
Apple(int i);
static const int apple_number;
void take(int num) const;
int add();
int add(int num) const;
int getCount() const;
};
#include "apple.h"
#include <iostream>
int Apple::apple_number = 10;
Apple::Apple(int i) { apple_number = i; }
int Apple::add() {
take(1);
return 0;
}
int Apple::add(int num) const {
take(num);
return num;
}
void Apple::take(int num) const {
std::cout << "take func " << num << std::endl;
}
int Apple::getCount() const {
take(1);
add(); // error
return apple_number;
}

View File

@@ -0,0 +1,11 @@
#pragma once
class Apple {
public:
Apple(int i);
static int apple_number;
void take(int num) const;
int add();
int add(int num) const;
int getCount() const;
};

View File

@@ -1,38 +1,12 @@
#include<iostream>
#include"apple.cpp"
using namespace std;
const int Apple::apple_number=10;
#include<iostream>
#include"apple.cpp"
#include "apple.h"
#include <iostream>
using namespace std;
Apple::Apple(int i):apple_number(i)
{
}
int Apple::add(){
take(1);
return 0;
}
int Apple::add(int num) const{
take(num);
return num;
}
void Apple::take(int num) const
{
cout<<"take func "<<num<<endl;
}
int Apple::getCount() const
{
take(1);
add(); // error
return apple_number;
}
int main(){
Apple a(2);
cout<<a.getCount()<<endl;
a.add(10);
// const Apple b(3);
// b.add(); // error
return 0;
int main() {
Apple a(2);
cout << a.getCount() << endl;
a.add(10);
// const Apple b(3);
// b.add(); // error
return 0;
}

View File

@@ -0,0 +1,17 @@
# please run `bazel run basic_content/const/class_const/static_example:main`
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library")
cc_library(
name = "apple",
srcs = ["apple.cpp"],
hdrs = ["apple.h"],
)
cc_binary(
name = "main",
srcs = ["main.cpp"],
deps = [
":apple",
],
copts = ["-std=c++11"]
)

View File

@@ -1,14 +1,22 @@
class Apple
{
private:
int people[100];
public:
Apple(int i);
static int ap; //在类实现文件中定义并初始化
static const int apple_number;
void take(int num) const;
int add(int num);
int add(int num) const;
int getCount() const;
};
#include "apple.h"
#include <iostream>
const int Apple::apple_number = 10;
int Apple::ap = 666;
Apple::Apple(int i) {}
int Apple::add(int num) {
take(num);
return 0;
}
int Apple::add(int num) const {
take(num);
return 0;
}
void Apple::take(int num) const {
std::cout << "take func " << num << std::endl;
}
int Apple::getCount() const {
take(1);
// add(); //error
return apple_number;
}

View File

@@ -0,0 +1,11 @@
#pragma once
class Apple {
public:
Apple(int i);
static int ap; // 在类实现文件中定义并初始化
static const int apple_number;
void take(int num) const;
int add(int num);
int add(int num) const;
int getCount() const;
};

View File

@@ -1,34 +1,11 @@
#include<iostream>
#include"apple.cpp"
using namespace std;
const int Apple::apple_number=10;
int Apple::ap=666;
Apple::Apple(int i)
{
}
int Apple::add(int num){
take(num);
}
int Apple::add(int num) const{
take(num);
}
void Apple::take(int num) const
{
cout<<"take func "<<num<<endl;
}
int Apple::getCount() const
{
take(1);
// add(); //error
return apple_number;
}
int main(){
Apple a(2);
cout<<a.getCount()<<endl;
cout<<a.ap<<endl;
a.add(10);
const Apple b(3);
b.add(100);
return 0;
#include "apple.h"
#include <iostream>
int main() {
Apple a(2);
std::cout << a.getCount() << std::endl;
std::cout << a.ap << std::endl;
a.add(10);
const Apple b(3);
b.add(100);
return 0;
}

View File

@@ -1,11 +1,9 @@
#include<iostream>
#include <iostream>
using namespace std;
void f(const int i){
i=10; // error: assignment of read-only parameter i
cout<<i<<endl;
void f(const int i) {
i = 10; // error: assignment of read-only parameter i
cout << i << endl;
}
int main(){
f(1);
}
int main() { f(1); }

View File

@@ -1,8 +1,8 @@
#include<iostream>
#include <iostream>
using namespace std;
int main(){
const int b = 10;
b = 0; //error
const string s = "helloworld";
const int i,j=0;
int main() {
const int b = 10;
b = 0; // error
const string s = "helloworld";
const int i, j = 0;
}

View File

@@ -0,0 +1,14 @@
# please run `bazel run basic_content/const/extern_const:const_file1` and `# please run `bazel run basic_content/const/extern_const:file`
load("@rules_cc//cc:defs.bzl", "cc_binary")
cc_binary(
name = "const_file1",
srcs = ["const_file1.cpp", "const_file2.cpp"],
copts = ["-std=c++11"]
)
cc_binary(
name = "file",
srcs = ["file1.cpp", "file2.cpp"],
copts = ["-std=c++11"]
)

View File

@@ -1 +1 @@
extern const int ext=12;
extern const int ext = 12;

View File

@@ -1,11 +1,8 @@
#include<iostream>
#include <iostream>
/**
* by 光城
* compile: g++ -o file const_file2.cpp const_file1.cpp
* execute: ./file
*/
extern const int ext;
int main(){
std::cout<<ext<<std::endl;
}
int main() { std::cout << ext << std::endl; }

View File

@@ -1 +1 @@
int ext;
int ext;

View File

@@ -1,11 +1,8 @@
#include<iostream>
#include <iostream>
/**
* by 光城
* compile: g++ -o file file2.cpp file1.cpp
* execute: ./file
*/
extern int ext;
int main(){
std::cout<<(ext+10)<<std::endl;
}
int main() { std::cout << (ext + 10) << std::endl; }

View File

@@ -0,0 +1,24 @@
# please run `bazel run basic_content/const/funciton_const/condition2:condition1`
# please run `bazel run basic_content/const/funciton_const/condition2:condition2`
# please run `bazel run basic_content/const/funciton_const/condition2:condition3`
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 = "condition1",
srcs = ["condition1.cpp"],
copts = ["-std=c++11"]
)
# Don't panic if you get compilation errors, this is what this code demonstrates, as expected.
cc_binary(
name = "condition2",
srcs = ["condition2.cpp"],
copts = ["-std=c++11"]
)
cc_binary(
name = "condition3",
srcs = ["condition3.cpp"],
copts = ["-std=c++11"]
)

View File

@@ -1,8 +1,7 @@
#include<iostream>
#include <iostream>
using namespace std;
int main(){
const int *ptr;
*ptr=10; //error
int main() {
const int *ptr;
*ptr = 10; // error
}

View File

@@ -1,9 +1,8 @@
#include<iostream>
#include <iostream>
using namespace std;
int main(){
const int p = 10;
const void *vp = &p;
void *vp = &p; //error
int main() {
const int p = 10;
const void *vp = &p;
void *vp = &p; // error
}

View File

@@ -1,12 +1,11 @@
#include<iostream>
#include <iostream>
using namespace std;
int main(){
const int *ptr;
int val = 3;
ptr = &val; //ok
int *ptr1 = &val;
*ptr1=4;
cout<<*ptr<<endl;
int main() {
const int *ptr;
int val = 3;
ptr = &val; // ok
int *ptr1 = &val;
*ptr1 = 4;
cout << *ptr << endl;
}

View File

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

View File

@@ -1,10 +1,9 @@
#include<iostream>
#include <iostream>
using namespace std;
int main(){
int num=0;
int * const ptr=&num; //const指针必须初始化且const指针的值不能修改
int * t = &num;
*t = 1;
cout<<*ptr<<endl;
int main() {
int num = 0;
int *const ptr = &num; // const指针必须初始化且const指针的值不能修改
int *t = &num;
*t = 1;
cout << *ptr << endl;
}

View File

@@ -1,8 +1,7 @@
#include<iostream>
#include <iostream>
using namespace std;
int main(){
const int num=0;
int * const ptr=&num; //error! const int* -> int*
cout<<*ptr<<endl;
int main() {
const int num = 0;
int *const ptr = &num; // error! const int* -> int*
cout << *ptr << endl;
}

View File

@@ -1,8 +1,7 @@
#include<iostream>
#include <iostream>
using namespace std;
int main(){
const int num=10;
const int * const ptr=&num; //error! const int* -> int*
cout<<*ptr<<endl;
int main() {
const int num = 10;
const int *const ptr = &num; // error! const int* -> int*
cout << *ptr << endl;
}

View File

@@ -0,0 +1,9 @@
# please run `bazel run basic_content/const/funciton_const/condition3:condition1`
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 = "condition1",
srcs = ["condition1.cpp"],
copts = ["-std=c++11"]
)

View File

@@ -1,10 +1,8 @@
#include<iostream>
#include <iostream>
using namespace std;
int main(){
const int p = 3;
const int * const ptr = &p;
cout<<*ptr<<endl;
int main() {
const int p = 3;
const int *const ptr = &p;
cout << *ptr << endl;
}