Giter Site home page Giter Site logo

stack-factory-puzzle's Introduction

Stack Factory Puzzle

When instances of classes derived from the same class need to be created based on certain conditions,
they are created on the heap. The puzzle is about how to create them on the stack.

In main, code between comments Begin End creates instances of classes X0 or X1 or X2 (depends on i) on the heap
(similar to a factory function which creates instances of classes on the heap and returns pointer to their base class).
Console output of the program is:

X0::X0 X0::Process: A   X0::~X0
X1::X1 X1::Process: ABC	X1::~X1
X2::X2 X2::Process: 123	X2::~X2

How to modify code between the comments Begin End, that it doesn't call new and console output is the same.

#include <iostream>
#include <string>
#include <memory>

struct X
{
  friend struct X0;
  friend struct X1;
  friend struct X2;
  
  virtual ~X() {}
  virtual void Process() = 0;
  
private:
  X() {}
};

struct X0 final: public X
{
  X0() { std::cout << "X0::X0 "; }
  ~X0() { std::cout << "\tX0::~X0\n"; }
    
  void Process() override { std::cout << "X0::Process: " << c_; }
  
  char c_ = 'A';
};

struct X1 final: public X
{
  X1() { std::cout << "X1::X1 "; }
  ~X1() { std::cout << "\tX1::~X1\n"; }
    
  void Process() override { std::cout << "X1::Process: " << s_; }
  
  std::string s_ = "ABC";
};

struct X2 final: public X
{
  X2() { std::cout << "X2::X2 "; }
  ~X2() { std::cout << "\tX2::~X2\n"; }
    
  void Process() override { std::cout << "X2::Process: " << i_; }
  
  int i_ = 123;
};

int main()
{
  for (auto i = 0; i < 3; i++) 
  {
    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> x{pX};    
    // End

    pX->Process();
  }

  return 0;
}

stack-factory-puzzle's People

Contributors

amarmer avatar

Watchers

James Cloos avatar Ivan Sanz Carasa avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.