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,13 @@
# please run `bazel run //practical_exercises/10_day_practice/day4/const:obj_func`
# please run `bazel run //practical_exercises/10_day_practice/day4/const:obj_ref`
load("@rules_cc//cc:defs.bzl", "cc_binary")
cc_binary(
name = "obj_func",
srcs = ["obj_func.cpp"],
)
cc_binary(
name = "obj_ref",
srcs = ["obj_ref.cpp"],
)

View File

@@ -0,0 +1,40 @@
#include <iostream>
using namespace std;
class R {
public:
R(int r1, int r2) {
R1 = r1;
R2 = r2;
}
// const区分成员重载函数
void print();
void print() const;
private:
int R1, R2;
};
/*
常成员函数说明格式:类型说明符 函数名参数表const;
这里const是函数类型的一个组成部分因此在实现部分也要带const关键字。
const关键字可以被用于参与对重载函数的区分
通过常对象只能调用它的常成员函数
*/
void R::print() {
cout << "普通调用" << endl;
cout << R1 << ":" << R2 << endl;
}
//实例化也需要带上
void R::print() const {
cout << "常对象调用" << endl;
cout << R1 << ";" << R2 << endl;
}
int main() {
R a(5, 4);
a.print(); //调用void print()
//通过常对象只能调用它的常成员函数
const R b(20, 52);
b.print(); //调用void print() const
return 0;
}

View File

@@ -0,0 +1,27 @@
#include <iostream>
using namespace std;
void display(const double &r);
class A {
public:
A(int i, int j) {
x = i;
y = j;
}
private:
int x, y;
};
int main() {
double d(9.5);
display(d);
A const a(3, 4); // a是常对象不能被更新
return 0;
}
void display(const double &r)
//常引用做形参,在函数中不能更新 r所引用的对象。
{
cout << r << endl;
}

View File

@@ -0,0 +1,8 @@
常类型的对象必须进行初始化,而且不能被更新。
常引用:被引用的对象不能被更新。
const 类型说明符 &引用名
常对象:必须进行初始化,不能被更新。
类名 const 对象名
常数组:数组元素不能被更新。
类型说明符 const 数组名[大小]...
常指针:指向常量的指针。