1 Star 0 Fork 115

thornphoenix / CPlusPlusThings

forked from Yj / CPlusPlusThings 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
lifetime.cpp 1.71 KB
一键复制 编辑 原始数据 按行查看 历史
light-city 提交于 2020-03-03 11:13 . update
//
// Created by light on 19-12-15.
//
#include <iostream>
using namespace std;
class shape {
public:
shape() { cout << "shape" << endl; }
~shape() {
cout << "~shape" << endl;
}
};
class circle : public shape {
public:
circle() { cout << "circle" << endl; }
~circle() {
cout << "~circle" << endl;
}
};
class triangle : public shape {
public:
triangle() { cout << "triangle" << endl; }
~triangle() {
cout << "~triangle" << endl;
}
};
class rectangle : public shape {
public:
rectangle() { cout << "rectangle" << endl; }
~rectangle() {
cout << "~rectangle" << endl;
}
};
class result {
public:
result() { puts("result()"); }
~result() { puts("~result()"); }
};
result process_shape(const shape &shape1, const shape &shape2) {
puts("process_shape()");
return result();
}
class Base {
public:
Base() {
cout << "Base()" << endl;
}
~Base() {
cout << "~Base()" << endl;
}
};
class Derived : public Base {
public:
Derived() {
cout << "Derived()" << endl;
}
~Derived() {
cout << "~Derived()" << endl;
}
};
string f() { return "abc"; }
void g() {
const string &s = f(); // still legal?
cout << s << endl;
}
Derived factory() {
return Derived();
}
int main() {
process_shape(circle(), triangle());
cout << endl;
// 临时对象延迟
// result &&r = process_shape(circle(), triangle());
// 临时对象延迟只对rvalue有用,而对xvalue无用!
// result &&r = std::move(process_shape(circle(), triangle()));
// const Base &b1 = factory();
Base *b1 = new Derived;
delete b1;
cout<<endl;
Derived d;
Base &b2 =d;
}
C++
1
https://gitee.com/thornphoenix_admin/CPlusPlusThings.git
git@gitee.com:thornphoenix_admin/CPlusPlusThings.git
thornphoenix_admin
CPlusPlusThings
CPlusPlusThings
master

搜索帮助