1 Star 0 Fork 0

gitstr/modern-cpp-tutorial

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
2.19.delegate.constructor.cpp 1.32 KB
一键复制 编辑 原始数据 按行查看 历史
Changkun Ou 提交于 2019-07-20 19:34 +08:00 . book: translate ch02
//
// 2.19.constructor.cpp
// chapter 2 language usability
// modern c++ tutorial
//
// created by changkun at changkun.de
// https://github.com/changkun/modern-cpp-tutorial
//
#include <iostream>
#include <string>
class Base {
public:
std::string str;
int value;
Base() = delete;
Base(std::string s) {
str = s;
}
// delegate constructor
Base(std::string s, int v) : Base(s) {
value = v;
}
// final constructor
virtual void foo() final {
return;
}
virtual void foo(int v) {
value = v;
}
};
class Subclass final : public Base {
public:
double floating;
Subclass() = delete;
// inherit constructor
Subclass(double f, int v, std::string s) : Base(s, v) {
floating = f;
}
// explifict constructor
virtual void foo(int v) override {
std::cout << v << std::endl;
value = v;
}
}; // legal final
// class Subclass2 : Subclass {
// }; // illegal, Subclass has final
// class Subclass3 : Base {
// void foo(); // illegal, foo has final
// }
int main() {
// Subclass oops; // illegal, default constructor has deleted
Subclass s(1.2, 3, "abc");
s.foo(1);
std::cout << s.floating << std::endl;
std::cout << s.value << std::endl;
std::cout << s.str << std::endl;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/gitstr/modern-cpp-tutorial.git
git@gitee.com:gitstr/modern-cpp-tutorial.git
gitstr
modern-cpp-tutorial
modern-cpp-tutorial
master

搜索帮助