473,385 Members | 1,622 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Object construction question

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<ArrayDataitems 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) ;
}
}
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 ?
May 2 '07 #1
6 1297
Bart Simpson 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<ArrayDataitems 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) ;
}
}
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 ?
What's the prototype of your PointerList<>::const_iterator? The compiler
is complaining that it cannot convert
*PointerList<ArrayData>::const_iterator to const std::string &

If const_iterator is simply a typedef T * const const_iterator, then it
all makes sense.

Fei
May 2 '07 #2
Bart Simpson wrote:
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 ?
Beacuse, as the compiler tells you, you are giving to the constructor a
ArrayData* const, not a const ArrayData&. Dereference two times:

ArrayData *arr = new ArrayData(**cit);
Regards,

Zeppe
May 2 '07 #3
On May 2, 8:26 am, Bart Simpson <123evergr...@terrace.comwrote:
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<ArrayDataitems 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.
May 2 '07 #4


Zeppe wrote:
Bart Simpson wrote:

>>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 ?


Beacuse, as the compiler tells you, you are giving to the constructor a
ArrayData* const, not a const ArrayData&. Dereference two times:

ArrayData *arr = new ArrayData(**cit);
Regards,

Zeppe
Dereferencing the variable (cit) twice fixed the problem. I understand
why I need the double dereferencing - however, I dont understand the
error message - thts what led me on a wild goose chase.

Whats with the "cannot convert parameter 1 from 'ArrayData *const ' to
'const std::string &" ?

I would ave thought that an error msg like this would have been closer
to the mark:

"cannot convert parameter 1 from 'ArrayData *const ' to 'const ArrayData&"

Any suggestions why the compiler was trying to cast the item to a string ?
May 2 '07 #5
Bart Simpson wrote:
Whats with the "cannot convert parameter 1 from 'ArrayData *const ' to
'const std::string &" ?

I would ave thought that an error msg like this would have been closer
to the mark:

"cannot convert parameter 1 from 'ArrayData *const ' to 'const ArrayData&"

Any suggestions why the compiler was trying to cast the item to a string ?
Well, the only thing I can think at is that when you don't give the
constructor an object that entails the call of the copy constructor, the
compiler assumes that you are going to call a normal constructor. And
since the only constructor that accepts one parameter is that one that
takes a string as first parameter, he's trying to use that, and to
convert the parameter to a string in order to call it.

Regards,

Zeppe
May 2 '07 #6
On Wed, 02 May 2007 14:26:00 +0200, Bart Simpson
<12**********@terrace.comwrote:
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 ;
};
Just a little note, unrelated to your question (which people more capable
than I already answered). Any method defined within the class definition
itself is implicitly inline, so there's no need to write it explicitly.

class A
{
int someFunc() { return 0; } // already inline
int otherFunc(); // not inline
int theRealFunc();
};

int
A::otherFunc()
{
return 1;
}

inline int
theRealFunc() // explicitly inline
{
return 5;
}

--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
May 2 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Anthony | last post by:
Hi, Can anyone help me out here, I'm looking for a design (pattern?) for a-synchronic construction of a class. I need this design in a framework which will run in a multithreaded system. I...
4
by: Jerivix Entadi | last post by:
I'm attempting to create an application to work with a fluid database of people. I'm doing this in a command line, text-based manner. I need to know two things: 1) How do I save data, perhaps a...
12
by: Olumide | last post by:
I'm studying Nigel Chapman's Late Night Guide to C++ which I think is an absolutely fantastic book; however on page 175 (topic: operator overlaoding), there the following code snippet: inline...
7
by: Dave | last post by:
Hello all, In the code below, I use a pointer to an object under construction. Is the usage below legal? I have come across similar code at work. It compiles, but I'm not sure it's really...
29
by: pmatos | last post by:
Hi all, Sometimes I have a function which creates an object and returns it. Some are sets, other vectors but that's not very important. In these cases I do something like this: vector<int> *...
6
by: Luke | last post by:
Here is my emails to Danny Goodman (but probably he is very busy so he didn't answered it). First email(simple): Subject: JavaScript Arrays " We all know the array can act like HashMap, but is...
8
by: Mark Neilson | last post by:
1. What is the best way to make a single instance of my top level class (DLL) internally available to all other members of the assembly? The top level object is where all other access is made in...
1
by: ChasW | last post by:
Im looking for some technical clarification regarding exactly what the differences, if any, are between the two pieces of code are, aside from the obvious syntactical differences. int x = 5; ...
4
by: craig | last post by:
During construction of an object "parent", if you create a subobject that stores a pointer to the parent (through the "this" pointer), will that pointer be valid when the subobject is later called?...
14
by: Jeroen | last post by:
Hi all, I've got a question about writing a library. Let me characterize that library by the following: * there is a class A which is available to the user * there is a class B that is used...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.