Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
programming:cpp-style-tips [2009/10/08 09:49]
cyril
programming:cpp-style-tips [2013/09/19 16:41] (current)
Line 248: Line 248:
  
 ===== Copy of abstract objects ===== ===== Copy of abstract objects =====
 +
 +==== Clone function ====
  
 You have an abstract class, and several implementations of this class. You have a pointer to the abstract class, but you don't know which implementations it is, and you want to make a copy of this object. The solution is to use a ''clone'' function. You have an abstract class, and several implementations of this class. You have a pointer to the abstract class, but you don't know which implementations it is, and you want to make a copy of this object. The solution is to use a ''clone'' function.
Line 258: Line 260:
 }; };
  
-class Type1Descriptor+class Type1Descriptor: public Descriptor
 { {
  public:  public:
   virtual Descriptor* clone()   virtual Descriptor* clone()
   {   {
-    Type1Descriptor *result = new Type1Descriptor(*this)+    return new Type1Descriptor(*this);
-    return result;+
   }   }
 }; };
Line 275: Line 276:
   {   {
     my_descriptor = modelDescriptor->clone();     my_descriptor = modelDescriptor->clone();
-    // I want a copy of the object, and not a reference, eg because I will customize it 
   }   }
 }; };
 </code> </code>
  
 +==== Factory model ====
 +
 +If you just want to copy the configuration parameters of the class, you can create a Factory class that will have the same constructor parameters than your class and will store them internally, and that has a ''create'' function that will create a copy of the class with these parameters.
 +
 +<code cpp>
 +class Descriptor
 +{
 + public:
 +  virtual void describe() = 0;
 +  Descriptor(int param1, int param2): param1(param1), param2(param2) { internalData = 0; }
 +};
 +
 +class DescriptorFactory
 +{
 + public:
 +  virtual Descriptor* create() const = 0;
 +}
 +
 +class Type1Descriptor: public Descriptor
 +{
 +  int param1;
 +  int param2;
 +  int internalData;
 + public:
 +  Descriptor(int param1, int param2): param1(param1), param2(param2) { internalData = 0; }
 +  virtual void describe() { ... }
 +};
 +
 +class Type1DescriptorFactory: public DescriptorFactory
 +{
 +  int param1;
 +  int param2;
 + public:
 +  Descriptor(int param1, int param2): param1(param1), param2(param2) {}
 +  virtual Descriptor* create() const
 +  {
 +    return new Type1Descriptor(param1, param2);
 +  }
 +}
 +
 +
 +class AnotherClass
 +{
 +  Descriptor *my_descriptor;
 + public:
 +  AnotherClass(DescriptorFactory *descriptorFactory)
 +  {
 +    my_descriptor = descriptorFactory->create();
 +  }
 +};
 +</code>
  
 ===== Inherited parameters ===== ===== Inherited parameters =====
programming/cpp-style-tips.1254995386.txt.gz · Last modified: 2013/09/19 16:43 (external edit)
CC Attribution-Share Alike 4.0 International
Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0