From 29780abd103a053240078935738a5cf7f8a95a55 Mon Sep 17 00:00:00 2001 From: Ivan Sanz-Carasa Date: Wed, 12 Sep 2018 13:11:52 +0200 Subject: [PATCH] solution: rvalues respect polymorphism --- README.md | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 8ab54c0..e131f6e 100644 --- a/README.md +++ b/README.md @@ -68,14 +68,15 @@ int main() X* pX = nullptr; // Begin - if (i == 0) - pX = new X0; - else if (i == 1) - pX = new X1; - else - pX = new X2; - - std::unique_ptr x{pX}; + X&& instance = + i == 0 ? (X&&)X0{} : + i == 1 ? (X&&)X1{} : + (X&&)X2{}; + + // we could get rid of ptrs and use normal calls + // instance.Process(); + + pX = &instance; // End pX->Process();