feat: delegate constructor
This commit is contained in:
parent
db87dfcf1c
commit
3c3d8d7c26
@ -8,7 +8,8 @@ add_library(
|
|||||||
src/range_based_for.cpp
|
src/range_based_for.cpp
|
||||||
src/structured_binding.cpp
|
src/structured_binding.cpp
|
||||||
src/type_inference.cpp
|
src/type_inference.cpp
|
||||||
src/variadic_templates.cpp)
|
src/variadic_templates.cpp
|
||||||
|
src/delegate_constructor.cpp)
|
||||||
|
|
||||||
target_compile_features(chapter_02 PUBLIC cxx_std_20)
|
target_compile_features(chapter_02 PUBLIC cxx_std_20)
|
||||||
|
|
||||||
|
10
chapters/chapter_02/include/delegate_constructor.h
Normal file
10
chapters/chapter_02/include/delegate_constructor.h
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "chapter_interface.h"
|
||||||
|
|
||||||
|
class DelegateConstructor : public ChapterTest {
|
||||||
|
virtual void run() override;
|
||||||
|
virtual const char *name() const override {
|
||||||
|
return "Delegate constructor test";
|
||||||
|
};
|
||||||
|
};
|
22
chapters/chapter_02/src/delegate_constructor.cpp
Normal file
22
chapters/chapter_02/src/delegate_constructor.cpp
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
#include "delegate_constructor.h"
|
||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
class Printer {
|
||||||
|
private:
|
||||||
|
std::string value;
|
||||||
|
|
||||||
|
public:
|
||||||
|
Printer(const std::string &value) : value(value){};
|
||||||
|
Printer(int value) : Printer(std::to_string(value)){};
|
||||||
|
|
||||||
|
void print() { std::cout << value << std::endl; }
|
||||||
|
};
|
||||||
|
|
||||||
|
void DelegateConstructor::run() {
|
||||||
|
std::cout << "print(\"hello\"): ";
|
||||||
|
Printer("hello").print();
|
||||||
|
|
||||||
|
std::cout << "print(23): ";
|
||||||
|
Printer(23).print();
|
||||||
|
}
|
@ -4,6 +4,7 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "constexp.h"
|
#include "constexp.h"
|
||||||
|
#include "delegate_constructor.h"
|
||||||
#include "if_constexpr.h"
|
#include "if_constexpr.h"
|
||||||
#include "ifswitch.h"
|
#include "ifswitch.h"
|
||||||
#include "initlist.h"
|
#include "initlist.h"
|
||||||
@ -30,6 +31,7 @@ int main(int argc, char **argv) {
|
|||||||
chapter_2_tests.emplace_back(std::make_unique<IfConstexprTest>());
|
chapter_2_tests.emplace_back(std::make_unique<IfConstexprTest>());
|
||||||
chapter_2_tests.emplace_back(std::make_unique<RangeBasedForTest>());
|
chapter_2_tests.emplace_back(std::make_unique<RangeBasedForTest>());
|
||||||
chapter_2_tests.emplace_back(std::make_unique<VariadicTemplates>());
|
chapter_2_tests.emplace_back(std::make_unique<VariadicTemplates>());
|
||||||
|
chapter_2_tests.emplace_back(std::make_unique<DelegateConstructor>());
|
||||||
|
|
||||||
for (auto &test : chapter_2_tests) {
|
for (auto &test : chapter_2_tests) {
|
||||||
std::cout << "\n" << test->name() << ":" << std::endl;
|
std::cout << "\n" << test->name() << ":" << std::endl;
|
||||||
|
Reference in New Issue
Block a user