Re: Object construction question
On May 2, 8:26 am, Bart Simpson <123evergr...@terrace.com> wrote:
I have the ff class decl:
class ArrayData : public myArray
{
public:
inline ArrayData(){ InitArray(&m_arr);}
inline explicit ArrayData(const std::string& name):m_name(name){
InitArray(&m_arr);}
inline explicit ArrayData(const ArrayData& arr):m_name(arr.m_name) {
CopyArray(&arr.m_arr, &m_arr) ;}
inline ArrayData& operator= (const ArrayData& rhs)
{
if (this != &rhs){ CopyArray(&rhs.m_arr, &m_arr) ; m_name = rhs.m_name ;}
return *this ;
}
inline ~ArrayData(){ FreeArray(&m_arr); }
inline const std::string& Name() const { return m_name ; }
private:
tpArray m_arr ;
std::string m_name ;
};
I also have a template class called PointerList<T>, which allows me to
manage a list of pointers generically. I made the copy/assignment ctors
for PointerList private.
I have a class (A) that uses a list of pointers of type ArrayData. I
need to implement a copy constructor for class A, and that requires a
member by member copy of the elements in the list...
I implemented this:
.....
//Copy PointerList<ArrayData> items element by element
if (!rhs.m_outarray_1ist.empty())
{
PointerList<ArrayData>::const_iterator cit = rhs.m_outarray_1ist.begin();
for (cit; cit != rhs.m_outarray_1ist.end(); cit++)
{
ArrayData *arr = new ArrayData(*cit); //Barfs here
m_outarray_1ist.push_back(arr) ;
Since the code shown is incomplete and since PointerList< ArrayData >,
instead of PointerList< ArrayData* >, is shown here, is
rhs.m_outarray_1ist.begin() returning an iterator to a pointer or an
iterator to an ArrayData element?
According to the error shown - its a pointer thats delivered by *cit
above (thats rather peculiar). Before you go "ahh, i get it -
dereference the dereferenced iterator is the solution" consider the
poor programmer who has to delve into this mess.
wild guess:
m_outarray_1ist.push_back( *cit ) ;
Then we'll start disscussing transfer of ownership here cause i see
lots of new's but no deletes.
At which point i'ld strongly suggest boost:shared_ptr.
}
}
However the compiler barfs at the line where the variable is being
created, with the error message:
cannot convert parameter 1 from 'ArrayData *const ' to 'const std::string &'
class ArrayData has a copy constructor that accepts a const ArrayData&,
so why does the compiler insist on casting it to a std::string (even
though I am using the 'explicit' keyword ?
ArrayData* const is a const pointer - what do you mean by 'casting'?
read the error: The compiler sees only one conversion ctor:
ArrayData(const std::string& name);
and it doesn't fit the bill.