I just realised that I have an issue with classes that can't be copy-assigned or move-assigned.
Suppose that I have a class named BroaderClass whose purpose is to perform several computations. It owns (amongst other things) a MyClass object named MyObj which is in charge of performing a part of the computations. If MyObj cannot be copy-assigned or move-assigned then it must be initialised in the initialisation list…
Alternatively, if it is absent from the initialiser list then the default constructor will be called and MyObj will have to be "overwritten" using copy- or move-assignment operator within the constructor's definition. For instance :
Code:
BroaderClass( [...] )
: [...] //No initialisation of MyObj here : default constructor will be called instead
{
MyObj = MyClass( [...] ); //"Overwrite" object using move assignment operator
}
However, if I initialise all the objects in the initialiser list, I end up with initialising lists that can be several hundreds lines long. Is this ok ?
For instance, my initialisation lists look something like this : (actual code)
Code:
Ell_XEqGradR_and_LaplREqDivA_per_dft_fftw::
Ell_XEqGradR_and_LaplREqDivA_per_dft_fftw
(/***/ DblArray & Xx
,/***/ DblArray & Xy
,/***/ DblArray & Ax
,/***/ DblArray & Ay
)
:_lX ( Xx._D.lX )
,_lY ( Xx._D.lY )
,_RlXx ( Xx )
,_RlXy ( Xy )
,_RlAx ( Ax )
,_RlAy ( Ay )
,_DFr(
{{ Xx._D.domResX , //
(Xx._D.domResY > 0 ? Xx._D.domResY/2 + 1 : -1 ) , //
(Xx._D.domResZ > 0 ? Xx._D.domResZ/2 + 1 : -1 ) }} ,
{{ Xx._D.lX , Xx._D.lY , Xx._D.lZ }} ,
{{ Xx._D.orgnX, Xx._D.orgnY, Xx._D.orgnZ }} )
)
,_FrXx (_DFr, "", "Ell_XEqGradR_and_LaplREqDivA_per_dft_fftw::_FrXx")
,_FrXy (_DFr, "", "Ell_XEqGradR_and_LaplREqDivA_per_dft_fftw::_FrXy")
,_FrAx (_DFr, "", "Ell_XEqGradR_and_LaplREqDivA_per_dft_fftw::_FrAx")
,_FrAy (_DFr, "", "Ell_XEqGradR_and_LaplREqDivA_per_dft_fftw::_FrAy")
//
,_frResX ( _FrXx.get_arrResX() )
,_frResY ( _FrXx.get_arrResY() )
//
,_PlanXxBwd( _FrXx, _RlXx, true )
,_PlanXyBwd( _FrXy, _RlXy, true )
,_PlanAxFwd( _RlAx, _FrAx, true )
,_PlanAyFwd( _RlAy, _FrAy, true )
{
//Do nothing
}
I find this very ugly but I am not sure about what to do else… The only alternative is to perform the initialisation within the constructor's definition, but that would require :
- a default constructor to be provided by MyClass, however the default constructor is implicitly deleted if MyClass owns a reference to an object/variable (unlike a pointer, a reference cannot be initialised to nullptr).
- all the objects owned by BroaderClass that aren't initialised in the initialiser list must be copy-assignable and/or move-assignable.
By moving stuff within the constructor it looks already much nicer (plus, I can fold the constructor's definition)
Code:
Ell_XEqGradR_and_LaplREqDivA_per_dft_fftw::
Ell_XEqGradR_and_LaplREqDivA_per_dft_fftw
(/***/ DblArray & Xx
,/***/ DblArray & Xy
,/***/ DblArray & Ax
,/***/ DblArray & Ay
)
:_lX ( Xx._D.lX )
,_lY ( Xx._D.lY )
,_RlXx ( & Xx )
,_RlXy ( & Xy )
,_RlAx ( & Ax )
,_RlAy ( & Ay )
{
_DFr = DomainRegCart(
{{ Xx._D.domResX , //
(Xx._D.domResY > 0 ? Xx._D.domResY/2 + 1 : -1 ) , //
(Xx._D.domResZ > 0 ? Xx._D.domResZ/2 + 1 : -1 ) }} ,
{{ Xx._D.lX , Xx._D.lY , Xx._D.lZ }} ,
{{ Xx._D.orgnX, Xx._D.orgnY, Xx._D.orgnZ }} );
//
_FrXx = CplArrayFftw(_DFr, "", "Ell_XEqGradR_and_LaplREqDivA_per_dft_fftw::_FrXx");
_FrXy = CplArrayFftw(_DFr, "", "Ell_XEqGradR_and_LaplREqDivA_per_dft_fftw::_FrXy");
_FrAx = CplArrayFftw(_DFr, "", "Ell_XEqGradR_and_LaplREqDivA_per_dft_fftw::_FrAx");
_FrAy = CplArrayFftw(_DFr, "", "Ell_XEqGradR_and_LaplREqDivA_per_dft_fftw::_FrAy");
//
_frResX = _FrXx.get_arrResX();
_frResY = _FrXx.get_arrResY();
//
_PlanXxBwd = Intf_fftw_dftReal_bwd( _FrXx, _RlXx, true );
_PlanXyBwd = Intf_fftw_dftReal_bwd( _FrXy, _RlXy, true );
_PlanAxFwd = Intf_fftw_dftReal_fwd( _RlAx, _FrAx, true );
_PlanAyFwd = Intf_fftw_dftReal_fwd( _RlAy, _FrAy, true );
}
However I require move-assignment operators in order to do this…
How do you usually write your constructors and their initialisation lists ? Having an object with a deleted copy- and/or move-constructor is not usually a problem right ?