1 Fegore

Copy Constructor And Assignment Operator Private Health

template<typename TValue>
class itk::VariableLengthVector< TValue >

Represents an array whose length can be defined at run-time.

This class is templated over the data type. This data-type is meant to be a scalar, such as float, double etc...

Note
ITK itself provides several classes that can serve as .
  • FixedArray - Compile time fixed length arrays that's intended to represent an enumerated collection of entities.
  • Array - Run time resizeable array that is intended to hold a collection of entities
  • Vector - Compile time fixed length array that is intended to hold a collection of data types. A vector usually has a mathematical meaning. It should only be used when mathematical operations such as addition, multiplication by a scalar, product etc make sense.
  • VariableLengthVector - Run time array that is intended to hold a collection of scalar data types. Again, it should be used only when mathematical operations on it are relevant. If not, use an Array.
  • Point - Represents the spatial coordinates of a spatial location. Operators on Point reflect geometrical concepts.
For the reasons listed above, you cannot instantiate
.
VariableLengthVector< bool >
Design Considerations: We do not derive from to avoid being limited by the explicit template instantiations of vnl_vector and other hacks that vnl folks have been forced to use.
Note
This work is part of the National Alliance for Medical Image Computing (NAMIC), funded by the National Institutes of Health through the NIH Roadmap for Medical Research, Grant U54 EB005149.
See Also
CovariantVector
SymmetricSecondRankTensor
RGBPixel
DiffusionTensor3D
Wiki Examples:
Invariant
If is true, is deletable (whether it's null or pointing to something with no elements. i.e. may be 0 and yet may be not null.)
Examples:
WikiExamples/ImageProcessing/UnaryFunctorImageFilter.cxx, WikiExamples/SimpleOperations/VariableLengthVector.cxx, WikiExamples/VectorImages/VectorImage.cxx, WikiExamples/VectorImages/VectorImageToImageAdaptor.cxx, and WikiExamples/VectorImages/VectorIndexSelectionCastImageFilter.cxx.

Definition at line 27 of file itkConstantBoundaryCondition.h.

Public Member Functions

TValue * AllocateElements (ElementIdentifier size) const
 
void DestroyExistingData ()
 
Self & FastAssign (const Self &v)
 
void Fill (TValue const &v)
 
const TValue * GetDataPointer () const
 
const TValue & GetElement (unsigned int i) const
 
RealValueType GetNorm () const
 
RealValueType GetSquaredNorm () const
 
bool IsAProxy () const
 
bool operator!= (const Self &v) const
 
Self & operator- ()
 
template<typename TExpr1 , typename TExpr2 , typename TBinaryOp >
Self & operator= (VariableLengthVectorExpression< TExpr1, TExpr2, TBinaryOp > const &rhs)
 
Self & operator= (const Self &v)
 
Self & operator= (TValue const &v)
 
bool operator== (const Self &v) const
 
TValue & operator[] (unsigned int i)
 
TValue const & operator[] (unsigned int i) const
 
void SetData (TValue *data, bool LetArrayManageMemory=false)
 
void SetData (TValue *data, unsigned int sz, bool LetArrayManageMemory=false)
 
void SetElement (unsigned int i, const TValue &value)
 
template<typename TReallocatePolicy , typename TKeepValuesPolicy >
void SetSize (unsigned int sz, TReallocatePolicy reallocatePolicy, TKeepValuesPolicy keepValues)
 
 VariableLengthVector ()
 
 VariableLengthVector (unsigned int dimension)
 
 VariableLengthVector (ValueType *data, unsigned int sz, bool LetArrayManageMemory=false)
 
 VariableLengthVector (const ValueType *data, unsigned int sz, bool LetArrayManageMemory=false)
 
 VariableLengthVector (const VariableLengthVector< TValue > &v)
 
template<typename TExpr1 , typename TExpr2 , typename TBinaryOp >
 VariableLengthVector (VariableLengthVectorExpression< TExpr1, TExpr2, TBinaryOp > const &rhs)
 
 ~VariableLengthVector ()
 
template<typename T >
 VariableLengthVector (const VariableLengthVector< T > &v)
 
void Swap (Self &v) noexcept
 
template<typename T >
Self & operator= (const VariableLengthVector< T > &v)
 
unsigned int Size (void) const
 
unsigned int GetSize (void) const
 
unsigned int GetNumberOfElements (void) const
 
void SetSize (unsigned int sz, bool destroyExistingData=true)
 
void Reserve (ElementIdentifier size)
 
Self & operator-- ()
 
Self & operator++ ()
 
Self operator-- (int)
 
Self operator++ (int)
 
template<typename T >
Self & operator-= (const VariableLengthVector< T > &v)
 
Self & operator-= (TValue s)
 
template<typename T >
Self & operator+= (const VariableLengthVector< T > &v)
 
Self & operator+= (TValue s)
 
template<typename TExpr1 , typename TExpr2 , typename TBinaryOp >
Self & operator+= (VariableLengthVectorExpression< TExpr1, TExpr2, TBinaryOp > const &rhs)
 
template<typename TExpr1 , typename TExpr2 , typename TBinaryOp >
Self & operator-= (VariableLengthVectorExpression< TExpr1, TExpr2, TBinaryOp > const &rhs)
 
template<typename T >
Self & operator*= (T s)
 
Self & operator*= (TValue s)
 
template<typename T >
Self & operator/= (T s)
 

Related Functions

(Note that these are not member functions.)

template<typename TExpr1 , typename TExpr2 >
mpl::EnableIf
< Details::op::CanBeMultiplied
< TExpr1, TExpr2 >
, VariableLengthVectorExpression
< TExpr1, TExpr2,
Details::op::Mult > >::Type 
operator* (TExpr1 const &lhs, TExpr2 const &rhs)
 
template<typename TExpr1 , typename TExpr2 >
mpl::EnableIf
< Details::op::CanBeAddedOrSubstracted
< TExpr1, TExpr2 >
, VariableLengthVectorExpression
< TExpr1, TExpr2,
Details::op::Plus > >::Type 
operator+ (TExpr1 const &lhs, TExpr2 const &rhs)
 
template<typename TExpr1 , typename TExpr2 >
mpl::EnableIf
< Details::op::CanBeAddedOrSubstracted
< TExpr1, TExpr2 >
, VariableLengthVectorExpression
< TExpr1, TExpr2,
Details::op::Sub > >::Type 
operator- (TExpr1 const &lhs, TExpr2 const &rhs)
 
template<typename TExpr1 , typename TExpr2 >
mpl::EnableIf
< Details::op::CanBeDivided
< TExpr1, TExpr2 >
, VariableLengthVectorExpression
< TExpr1, TExpr2,
Details::op::Div > >::Type 
operator/ (TExpr1 const &lhs, TExpr2 const &rhs)
 
template<typename TValue >
std::ostream & operator<< (std::ostream &os, const VariableLengthVector< TValue > &arr)
 
Standard compliance functions
template<typename T >
void swap (VariableLengthVector< T > &l_, VariableLengthVector< T > &r_) noexcept
 

Member Typedef Documentation

Constructor & Destructor Documentation

Default constructor. It is created with an empty array it has to be allocated later by assignment, or .

Postcondition
is null
is 0
is true
template<typename TValue>

Constructor with size. Size can only be changed by assignment, or .

Postcondition
is not null and points to an array of , even if is 0
values are left uninitialized.
is
is true
template<typename TValue>

Constructor that initializes array with contents from a user supplied buffer. The pointer to the buffer and the length is specified. By default, the array does not manage the memory of the buffer. It merely points to that location and it is the user's responsibility to delete it. If is true, then this class will free the memory when this object is destroyed.

Postcondition
values are left unmodified
template<typename TValue>

Constructor that initializes array with contents from a user supplied buffer. The pointer to the buffer and the length is specified. By default, the array does not manage the memory of the buffer. It merely points to that location and it is the user's responsibility to delete it. If is true, then this class will free the memory when this object is destroyed.

Warning
This overload receives a non-modiable array, and yet it will let the end-user try to modify it through interface. Use this constructor with care as this may lead to undefined behaviour. Prefer using instead of in case we which to use this constructor.
Postcondition
values are left unmodified
template<typename TValue>
template<typename TValue>
template<typename T >

Copy constructor. The reason why the copy constructor and the assignment operator are templated is that it will allow implicit casts to be performed. For instance:

VariableLengthVector< int > vI;
VariableLengthVector< float > vF( vI );
or for instance vF = static_cast< VariableLengthVector< float > >( vI );
Note
However that static casting in this way will imply the allocation of a temporary . Prefer to directly use the assignment converting operator in code where uses of would be required.
Postcondition
is not null and points to an array of , if is 0, otherwise it's null.
values are left uninitialized.
is
is true

Definition at line 393 of file itkVariableLengthVector.h.

References itk::VariableLengthVector< TValue >::Size().

Copy constructor. Overrides the default non-templated copy constructor that the compiler provides.

Postcondition
is not null and points to an array of , if is 0, otherwise it's null.
values are left uninitialized.
is
is true
template<typename TValue>
template<typename TValue>
template<typename TExpr1 , typename TExpr2 , typename TBinaryOp >

Constructor from an Expression Template vector.

Template Parameters
TExpr1Type of the left sub-expression
TExpr2Type of the right sub-expression
TBinaryOpBinary Operation to apply to both sub-expressions.
Parameters
[in]rhsNon evaluated Expression Template.

Builds the new with an expression template. The code loops over all components from the template expression, and evaluates them on the fly to fill the content of the new vector.

Postcondition
is not null and points to an array of , even if is 0
is
is true

This destructor is not virtual for performance reasons. However, this means that subclasses cannot allocate memory.

template<typename TValue>

Member Function Documentation

Allocate memory of certain size and return it.

Returns
a non-null pointer to an array of elements (0 is a valid parameter).
template<typename TValue>

Destroy data that is allocated internally, if is true.

template<typename TValue>

Fast Assignment.

Precondition
is true: the is not a proxy, checked with an assertion. Call to ensure a vector is not a proxy anymore.
current size is identical to the one from the right hand side operand, checked with an assertion.
Doesn't not support empty vectors.
template<typename TValue>

Set the all the elements of the array to the specified value.

Precondition
This function may be called on empty vectors, it's a no-op.

Referenced by itk::MaskImageFilter< TInputImage, TMaskImage, TOutputImage >::CheckOutsideValue(), itk::LinearInterpolateImageFunction< TInputImage, TCoordRep >::MakeZeroInitializer(), itk::NumericTraits< VariableLengthVector< T > >::max(), itk::NumericTraits< VariableLengthVector< T > >::min(), itk::NumericTraits< VariableLengthVector< T > >::NonpositiveMin(), itk::NumericTraits< VariableLengthVector< T > >::OneValue(), itk::NumericTraits< VariableLengthVector< T > >::SetLength(), and itk::NumericTraits< VariableLengthVector< T > >::ZeroValue().

template<typename TValue>

Returns vector's squared Euclidean Norm

template<typename TValue>
template<typename TValue>
template<typename T >

Multiply each element of the vector by a scalar 's'. The scalar value is cast to the current vector element type prior to multiplication.

Exceptions

Definition at line 884 of file itkVariableLengthVector.h.

template<typename TValue>
template<typename T >

Element-wise addition of vector 'v' to the current vector. The vectors do not have to have the same element type. The input vector elements are cast to the current vector element type before the addition is performed.

Exceptions
Note
For efficiency, the length of the vectors is not checked; they are assumed to have the same length.

Definition at line 814 of file itkVariableLengthVector.h.

template<typename TValue>
template<typename TExpr1 , typename TExpr2 , typename TBinaryOp >

Compound addition operator with a expression template vector.

Template Parameters
TExpr1Type of the left sub-expression
TExpr2Type of the right sub-expression
TBinaryOpBinary Operation to apply to both sub-expressions.
Parameters
[in]rhsNon evaluated Expression Template.
Precondition
, checked with an assertion
Note
The elements of the expression template are evaluated one by one.

Definition at line 846 of file itkVariableLengthVector.h.

Negates each vector element.

Warning
This operator has a non standard semantics. Instead of returning a new , it modifies the current object.
template<typename TValue>
template<typename TValue>
template<typename T >

Element-wise subtraction of vector 'v' from the current vector. The vectors do not have to have the same element type. The input vector elements are cast to the current vector element type before the subtraction is performed.

Exceptions
Note
For efficiency, the length of the vectors is not checked; they are assumed to have the same length.

Definition at line 782 of file itkVariableLengthVector.h.

template<typename TValue>
template<typename TExpr1 , typename TExpr2 , typename TBinaryOp >

Compound substraction operator with a expression template vector.

Template Parameters
TExpr1Type of the left sub-expression
TExpr2Type of the right sub-expression
TBinaryOpBinary Operation to apply to both sub-expressions.
Parameters
[in]rhsNon evaluated Expression Template.
Precondition
, checked with an assertion
Note
The elements of the expression template are evaluated one by one.

Definition at line 867 of file itkVariableLengthVector.h.

template<typename TValue>
template<typename T >

Divide vector elements by a scalar 's'. The vector does not have to have the same element type as the scalar type. Both the scalar and vector elements are cast to the RealValueType prior to division, and the result is cast to the ValueType.

Exceptions

Definition at line 915 of file itkVariableLengthVector.h.

template<typename TValue>
template<typename TExpr1 , typename TExpr2 , typename TBinaryOp >

Assignment from an Expression Template vector.

Template Parameters
TExpr1Type of the left sub-expression
TExpr2Type of the right sub-expression
TBinaryOpBinary Operation to apply to both sub-expressions.
Parameters
[in]rhsNon evaluated Expression Template.

Resets the new

template<typename TValue>
template<typename TValue>
template<typename TValue>
template<typename TValue>
template<typename TValue>
template<typename TValue>
template<typename TValue>
template<typename TValue>

In some cases, an instance of a C++ class should not be copied at all. There are three ways to prevent such an object copy: keeping the copy constructor and assignment operator private, using a special non-copyable mixin, or deleting those special member functions.

A class that represents a wrapper stream of a file should not have its instance copied around. It will cause a confusion in the handling of the actual I/O system. In a similar spirit, if an instance holds a unique private object, copying the pointer does not make sense. A somehow related problem but not necessarily similar is the issue of object slicing.

The following illustration demonstrates a simple class that is supposed to have a unique owner, an instance of .

For this purpose, the implementation of is as simple as:

To show the issue, a helper function is implement as follows:

From this example, it is obvious that an instance of must not be copied. In particular, another clone of a similar car should not automatically belong to the same owner. In fact, running the subsequent code:

will give the output:

Owner is Joe Sixpack Owner is Joe Sixpack

How can we prevent this accidental object copy?

Method 1: Private copy constructor and copy assignment operator

A very common technique is to declare both the copy constructor and copy assignment operator to be private. We do not even need to implement them. The idea is so that any attempt to perform a copy or an assignment will provoke a compile error.

In the above example, will be modified to look like the following. Take a look closely at two additional private members of the class.

Now if we try again to assign an instance of to a new one, the compiler will complain loudly:

example.cpp:35:22: error: calling a private constructor of class 'Car' Car anotherSedan = sedan; ^ example.cpp:22:3: note: declared private here Car(const Car&); ^ 1 error generated.

If writing two additional lines containing repetitive names is too cumbersome, a macro could be utilized instead. This is the approach used by WebKit, see its macro from wtf/Noncopyable.h (do not be alarmed, in the context of WebKit source code, WTF here stands for Web Template Framework). Chromium code, as shown in the file base/macros.h, distinguishes between copy constructor and assignment, denoted as and macros, respectively.

Method 2: Non-copyable mixin

The idea above can be extended to create a dedicated class which has the sole purpose to prevent object copying. It is often called as Noncopyable and typically used as a mixin. In our example, the class can then be derived from this .

Boost users may be already familiar with boost::noncopyable, the Boost flavor of the said mixin. A conceptual, self-contained implementation of that mixin will resemble something like the following:

Our lovely Car class can be written as:

Compared to the first method, using has the benefit of making the intention very clear. A quick glance at the class, right on its first line, and you know right away that its instance is not supposed to be copied.

Method 3: Deleted copy constructor and copy assignment operator

For modern applications, there is less and less reason to get stuck with the above workaround. Thanks to C++11, the solution becomes magically simple: just delete the copy constructor and assignment operator. Our class will look like this instead:

Note that if you use mixin with a compiler supporting C++11, the implementation of also automatically deletes the said member functions.

With this approach, any accidental copy will result in a quite friendlier error message:

example.cpp:34:7: error: call to deleted constructor of 'Car' Car anotherSedan = sedan; ^ ~~~~~ example.cpp:10:3: note: 'Car' has been explicitly marked deleted here Car(const Car&) = delete; ^

So, which of the above three methods is your favorite?

Leave a Comment

(0 Comments)

Your email address will not be published. Required fields are marked *