The problem solved:
Use the prototype instance to specify the kind of object to create and create a new object by copying the prototypes.
This is in fact the role of the copy constructor of C + + is consistent, in fact, the dynamic extraction of the current object runtime state (well, I have not yet understood the difference between the two, please Daniel Enlighten).
The copy constructors of C + + are divided into shallow and deep copies.
Shallow copy: It is to copy each member variable in the object, which is to assign the variables in the A1 class directly to the variables in the A2 class, which is the value passing, but involves the pointer type, they point to the same piece of memory. There is a problem: when B releases the memory (for example, a destructor), a pointer inside a is a wild pointer, and a run error occurs.
Deep copy: It is not only the use of value to pass, but each variable has its own independent memory space, non-interference.
Prototype mode UML class diagram:
The following code is excerpted from: "C + + Design mode-prototype mode"
/** * filename:prototypepatterndemo** author:jelly young** date:2013/11/25** description:more Information, go tohttp://www.jellythink.com*/#include<iostream>using namespacestd;//Interfaceclassprototype{ Public: Prototype () {}Virtual~Prototype () {}VirtualPrototype * Clone () =0;};//ImplementclassConcreteprototype: Publicprototype{ Public: Concreteprototype (): M_counter (0){} Virtual~Concreteprototype () {}//copy ConstructorConcreteprototype (ConstConcreteprototype &RHS) {M_counter=RHS. M_counter; } //copying itself VirtualConcreteprototype *Clone () {//Call copy Constructor return NewConcreteprototype (* This ); }Private : intM_counter;};intMainintARGC,Char**argv) { //generate a pair of imagesConcreteprototype * Conproa =NewConcreteprototype (); //copying itselfConcreteprototype * Conprob = conproa->Clone (); Delete Conproa; Conproa=NULL; Delete Conprob; Conprob=NULL; return 0;}
Common scenarios
Basically, you need to use prototype mode to get an instance of a that is the same as a content, but does not interfere with each other.
For example (may be wrong ...):
When we play a copy of the game, some games are designed to save the current character state before the copy, and then restore the state of the forward copy after the copy is finished, is it possible to apply the prototype mode at this time? Copy an instance of the current role before the copy, and then use the copied new instance to maintain the role after the copy is made, and the old instance is used when the copy is exited ...
10. [C + +] prototype mode