1 Star 2 Fork 0

岑剑伟 / CPlusPlusThings

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
lifetime.cpp 1.17 KB
一键复制 编辑 原始数据 按行查看 历史
//
// Created by light on 19-12-15.
//
#include <iostream>
#include "shape.h"
using namespace std;
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/genvex/CPlusPlusThings.git
git@gitee.com:genvex/CPlusPlusThings.git
genvex
CPlusPlusThings
CPlusPlusThings
master

搜索帮助

53164aa7 5694891 3bd8fe86 5694891