If the allocated array always has size equal to 1 then the constructor will look as
Test(const Test & rhs) : id( rhs.id )
{
prix = new int[1];
*prix = *rhs.prix;
}
Or if the compiler supports initializer lists then the constructor can be written as
Test(const Test & rhs) : id( rhs.id )
{
prix = new int[1] { *rhs.prix };
}
Otherwise the class has to have an additional data member that will contain the number of elements in the array.
Let assume that size_t size
is such a data member. Then the constructor will look as
#include <algorithm>
//...
Test(const Test & rhs) : id( rhs.id ), size( rhs.size )
{
prix = new int[size];
std::copy( rhs.prix, rhs.prix + rhs.size, prix );
}
You could write for example as
Test(const Test & rhs) : id( rhs.id ), size( rhs.size ), prix( new int[size] )
{
std::copy( rhs.prix, rhs.prix + rhs.size, prix );
}
but in this case data member size has to be defined before data member prix in the class definition.