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,15 @@
# please run `bazel run basic_content/pointer_refer:copy_construct`
# please run `bazel run basic_content/pointer_refer:effec`
load("@rules_cc//cc:defs.bzl", "cc_binary")
cc_binary(
name = "copy_construct",
srcs = ["copy_construct.cpp"],
copts = ["-std=c++11", "-fno-elide-constructors"]
)
cc_binary(
name = "effec",
srcs = ["effec.cpp"],
copts = ["-std=c++11"]
)

View File

@@ -12,24 +12,18 @@ using namespace std;
class Copyable {
public:
Copyable(){}
Copyable(const Copyable &o) {
cout << "Copied" << endl;
}
Copyable() {}
Copyable(const Copyable &o) { cout << "Copied" << endl; }
};
Copyable ReturnRvalue() {
return Copyable(); //返回一个临时对象
}
void AcceptVal(Copyable a) {
}
void AcceptRef(const Copyable& a) {
return Copyable(); // 返回一个临时对象
}
void AcceptVal(Copyable a) {}
void AcceptRef(const Copyable &a) {}
int main() {
cout << "pass by value: " << endl;
AcceptVal(ReturnRvalue()); // 应该调用两次拷贝构造函数
cout << "pass by reference: " << endl;
AcceptRef(ReturnRvalue()); //应该只调用一次拷贝构造函数
cout << "pass by value: " << endl;
AcceptVal(ReturnRvalue()); // 应该调用两次拷贝构造函数
cout << "pass by reference: " << endl;
AcceptRef(ReturnRvalue()); // 应该只调用一次拷贝构造函数
}

Binary file not shown.

View File

@@ -1,22 +1,20 @@
#include<iostream>
#include <iostream>
using namespace std;
void test1(int* p)
{
*p = 3; //此处应该首先判断p是否为空为了测试的需要此处我们没加。
return;
void test1(int *p) {
*p = 3; //此处应该首先判断p是否为空为了测试的需要此处我们没加。
return;
}
void test2(int& p)
{
p = 3; //此处应该首先判断p是否为空为了测试的需要此处我们没加。
return;
void test2(int &p) {
p = 3; //此处应该首先判断p是否为空为了测试的需要此处我们没加。
return;
}
int main() {
int a=10;
int *p=&a;
test1(p);
test2(a);
cout<<a<<endl;
return 0;
int a = 10;
int *p = &a;
test1(p);
test2(a);
cout << a << endl;
return 0;
}