# Does an efficient cloning of multiple different classes with an override of InternalClone() exist?

**URL:** <https://discourse.itk.org/t/does-an-efficient-cloning-of-multiple-different-classes-with-an-override-of-internalclone-exist/1109>\
**Category:** Algorithms\
**Tags:** clone\
**Created:** [July 11, 2018, 12:35pm UTC](https://discourse.itk.org/t/does-an-efficient-cloning-of-multiple-different-classes-with-an-override-of-internalclone-exist/1109 "2018-07-11T12:35:09Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![Gordian](https://discourse.itk.org/user_avatar/discourse.itk.org/gordian/32/335_2.png) [@Gordian](https://discourse.itk.org/u/Gordian)\
**Post date:** [July 12, 2018, 9:19am UTC](https://discourse.itk.org/t/does-an-efficient-cloning-of-multiple-different-classes-with-an-override-of-internalclone-exist/1109/2 "2018-07-12T09:19:12Z")

</div>

Hi,

now i made a try to get a clone of the [itk::AmoebaOptimizer](https://itk.org/Doxygen/html/classitk_1_1AmoebaOptimizer.html#a4a93d39a4a5a4d36cbe057ca6861c54e).

To get a clone with the correct parameters I added in following classes following code.  
In [itkOptimizer.h](https://github.com/InsightSoftwareConsortium/ITK/blob/master/Modules/Numerics/Optimizers/include/itkOptimizer.h), [itkSingleValuedNonLinearVnlOptimizer.h](https://github.com/InsightSoftwareConsortium/ITK/blob/master/Modules/Numerics/Optimizers/include/itkSingleValuedNonLinearVnlOptimizer.h), [itkAmoebaOptimizer.h](https://github.com/InsightSoftwareConsortium/ITK/blob/master/Modules/Numerics/Optimizers/include/itkAmoebaOptimizer.h) I added in the protected space:

```
/**
* Clone the current optimizer.
* This does a complete copy of the optimizer state to the new optimizer
*/
typename LightObject::Pointer InternalClone() const override;

```

In the body files is added:

[itkOptimizer.cxx](https://github.com/InsightSoftwareConsortium/ITK/blob/master/Modules/Numerics/Optimizers/src/itkOptimizer.cxx)

```
LightObject::Pointer
Optimizer
::InternalClone() const
{
	// Default implementation just copies the parameters from
	// this to new optimizer.
	LightObject::Pointer loPtr = Superclass::InternalClone();
	Self::Pointer rval = dynamic_cast<Self *>(loPtr.GetPointer());
	if (rval.IsNull())
	{
		itkExceptionMacro(<< "downcast to type " << this->GetNameOfClass() << " failed.");
	}

	rval->SetScales(this->GetScales());
	rval->SetInitialPosition(this->GetInitialPosition());
	return loPtr;
}

```

[itkSingleValuedNonLinearVnlOptimizer.cxx](https://github.com/InsightSoftwareConsortium/ITK/blob/master/Modules/Numerics/Optimizers/src/itkSingleValuedNonLinearVnlOptimizer.cxx)

```
LightObject::Pointer
	SingleValuedNonLinearVnlOptimizer
	::InternalClone() const
	{
		// Default implementation just copies the parameters from
		// this to new optimizer.
		LightObject::Pointer loPtr = Superclass::InternalClone();

		Self::Pointer rval = dynamic_cast<Self *>(loPtr.GetPointer());
		if (rval.IsNull())
		{
			itkExceptionMacro(<< "downcast to type " << this->GetNameOfClass() << " failed.");
		}
		rval->SetMaximize(this->GetMaximize());
		return loPtr;
	}

```

[itkAmoebaOptimizer.cxx](https://github.com/InsightSoftwareConsortium/ITK/blob/master/Modules/Numerics/Optimizers/src/itkAmoebaOptimizer.cxx)

```
LightObject::Pointer
AmoebaOptimizer
::InternalClone() const
{
	// Default implementation just copies the parameters from
	// this to new optimizer.
	LightObject::Pointer loPtr = Superclass::InternalClone();

	Self::Pointer rval = dynamic_cast<Self *>(loPtr.GetPointer());
	if (rval.IsNull())
	{
		itkExceptionMacro(<< "downcast to type " << this->GetNameOfClass() << " failed.");
	}
	
	rval->SetMaximumNumberOfIterations(this->GetMaximumNumberOfIterations());
	rval->SetOptimizeWithRestarts(this->GetOptimizeWithRestarts());
	rval->SetParametersConvergenceTolerance(this->GetParametersConvergenceTolerance());
	rval->SetFunctionConvergenceTolerance(this->GetFunctionConvergenceTolerance());
	rval->SetInitialSimplexDelta(this->GetInitialSimplexDelta());
	rval->SetAutomaticInitialSimplex(this->GetAutomaticInitialSimplex());

	return loPtr;
}

```

I tested it in my local copy and it worked as expected. I have a local copy but cannot push it to the repository. Even pushing to a fork doesnt work.

Is this something I can contribute or do you need more information?  
Do I have to follow this guide for contribution: [itk.org/Wiki/ITK/Git/Develop](https://itk.org/Wiki/ITK/Git/Develop)?

---

_[View the full topic](https://discourse.itk.org/t/does-an-efficient-cloning-of-multiple-different-classes-with-an-override-of-internalclone-exist/1109)._
