online compiler and debugger for c/c++

code. compile. run. debug. share.
Source Code    Language
/****************************************************************************** Welcome to GDB Online. GDB online is an online compiler and debugger tool for C, C++, Python, PHP, Ruby, C#, OCaml, VB, Perl, Swift, Prolog, Javascript, Pascal, COBOL, HTML, CSS, JS Code, Compile, Run and Debug online from anywhere in world. Here we introduce a varadic template functor for the factory method, and we use partial template specialization on the functor class *******************************************************************************/ #include <iostream> #include <string> #include <vector> #include <memory> using namespace std; class Animal { public: Animal () {} Animal (string name):_name (name) {} virtual ~ Animal () {} virtual void action () = 0; string name () { return _name; } private: string _name = {"anonymous"}; }; class Cat:public Animal { public: //constructor with two arguments Cat (const string & name, const string & color) :Animal (name), _color (color) {} void action () { cout << "I am a cat, my name is " << name () << " and my fur color is " << _color << endl; } private: string _color = {"undefined"}; }; class Bug:public Animal { public: //constructor with three arguments Bug (const string & name, const string & color, int legs) :Animal (name), _color (color), _legs (legs) {} void action () { cout << "I am a bug, my name is " << name () << " and my skin color is " << _color << " and I jump on " << _legs << " legs"<<endl; } private: string _color = {"undefined"}; int _legs = { 0 }; }; class UnknownFormOfLife:public Animal { public: void action () { cout << "I am an unknown form of life" << endl; } }; // partial specialization for a varadic template class method // The template class class makeAnimalFunctor is a functor //default implementation -> creates an UnknownFormOfLife template<typename T, typename... Ts> struct makeAnimalFunctor { std::unique_ptr<Animal> operator()() { return make_unique<UnknownFormOfLife>(); } }; //partial specialization for Cat template<typename... Ts> struct makeAnimalFunctor<Cat,Ts...> { std::unique_ptr<Animal> operator()(Ts&&... args) { return make_unique<Cat>(std::forward<Ts>(args)...); } }; //partial specialization for Bug template<typename... Ts> struct makeAnimalFunctor<Bug,Ts...> { std::unique_ptr<Animal> operator()(Ts&&... args) { return make_unique<Bug>(std::forward<Ts>(args)...); } }; int main () { vector < shared_ptr < Animal >> animals; animals.push_back(makeAnimalFunctor<Cat,string,string>()("Charlie","Black")); animals.push_back(makeAnimalFunctor<Bug,string,string,int>()("Tom","green",8)); animals.push_back(makeAnimalFunctor<UnknownFormOfLife>()()); for (auto a : animals) a->action(); }

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