support bazel complie this project and format code.
This commit is contained in:
15
basic_content/pointer_refer/BUILD
Normal file
15
basic_content/pointer_refer/BUILD
Normal 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"]
|
||||
)
|
Binary file not shown.
@@ -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.
@@ -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;
|
||||
}
|
||||
|
Reference in New Issue
Block a user