online compiler and debugger for c/c++

code. compile. run. debug. share.
Source Code   
Language
#include <iostream> #include <memory> #include <optional> struct Base { virtual void f() = 0; virtual ~Base() {}; }; struct Helper: Base { void f() override { std::cout << "Helper" << std::endl; } }; struct Derived: Base { Derived(Helper& helper): m_helper(helper) {} void f() override { std::cout << "Derived" << std::endl; } Helper& m_helper; }; void test1(bool condition) { auto h = std::make_shared<Helper>(); std::shared_ptr<Base> p; if (condition) { p = std::make_shared<Derived>(*h); } else { p = h; } p->f(); } void test2(bool condition) { auto h = std::make_shared<Helper>(); auto p = condition ? std::shared_ptr<Base>(new Derived(*h)) : h; p->f(); } void test3(bool condition) { Helper h; std::optional<Derived> d; if (condition) d.emplace(h); Base& p = d ? *d : static_cast<Base&>(h); p.f(); } int main() { test1(true); test1(false); test2(true); test2(false); test3(true); test3(false); return 0; }

Compiling Program...

Command line arguments:
Standard Input: Interactive Console Text

                

                

Program is not being debugged. Click "Debug" button to start program in debug mode.

#FunctionFile:Line
VariableValue
RegisterValue
ExpressionValue