before.cxx
#include <iostream>
class Factorial {
public:
Factorial(unsigned int n) {
double value = 1;
for (unsigned int k = 2; k < n; ++k) {
value *= k;
}
this->value = value;
}
void displayValue(void) {
std::cout << this->value << std::endl;
}
private:
double value;
};
int main(void) {
Factorial factorial1(100);
factorial1.displayValue();
Factorial factorial2(100);
factorial2.displayValue();
Factorial factorial3(100);
factorial3.displayValue();
return 0;
}
after.cxx
#include <iostream>
class Prototype {
public:
virtual Prototype *clone(void) = 0;
};
class Factorial: public Prototype {
public:
Factorial(unsigned int n) {
double value = 1;
for (unsigned int k = 2; k < n; ++k) {
value *= k;
}
this->value = value;
}
void displayValue(void) {
std::cout << this->value << std::endl;
}
Prototype *clone(void) {
Factorial *copy = reinterpret_cast<Factorial *>(new unsigned char[sizeof Factorial]);
*copy = *this;
return copy;
}
private:
double value;
};
int main(void) {
Factorial factorial1(100);
factorial1.displayValue();
Factorial *factorial2 = static_cast<Factorial *>(factorial1.clone());
factorial2->displayValue();
delete factorial2;
Factorial *factorial3 = static_cast<Factorial *>(factorial1.clone());
factorial3->displayValue();
delete factorial3;
return 0;
}