1
0
Fork 0

feat: delegate constructor

main
mandlm 2023-11-05 11:54:41 +01:00
parent db87dfcf1c
commit 3c3d8d7c26
Signed by: mandlm
GPG Key ID: 4AA25D647AA54CC7
4 changed files with 36 additions and 1 deletions

View File

@ -8,7 +8,8 @@ add_library(
src/range_based_for.cpp
src/structured_binding.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)

View 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";
};
};

View 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();
}

View File

@ -4,6 +4,7 @@
#include <vector>
#include "constexp.h"
#include "delegate_constructor.h"
#include "if_constexpr.h"
#include "ifswitch.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<RangeBasedForTest>());
chapter_2_tests.emplace_back(std::make_unique<VariadicTemplates>());
chapter_2_tests.emplace_back(std::make_unique<DelegateConstructor>());
for (auto &test : chapter_2_tests) {
std::cout << "\n" << test->name() << ":" << std::endl;