Confused by const_iterator

M

magnus.moraberg

Hi,

I have the following code which fails to compile -

MField<int> mFieldInt;
mFieldInt.insert(mFieldInt.begin(),10);

because mFieldInt.begin() returns a const_iterator. The following code
does compile -

MField<int>::iterator i; // how do I initialize i?
mFieldInt.insert(i,10);

MField<int> stores a number of ints internally using a vector. Here's
the specification for MField -

http://www.h3dapi.org/uploads/api/H3DAPI_20_beta/docs/H3DAPI/html/classH3D_1_1MField.html

My question though is, why does MField have an insert function
associated with it if its begin function returns a const_iterator? How
can one insert something into a MField object when the only function
that returns an iterator (instead of a const_iterator) is the insert
function itself. I'm totally confused by this.

How can I insert a value at the start of an MField object's internal
vector?

Thanks for your help,

Barry
 
D

DerTopper

Hi,

I have the following code which fails to compile -

MField<int> mFieldInt;
mFieldInt.insert(mFieldInt.begin(),10);

because mFieldInt.begin() returns a const_iterator. The following code
does compile -

MField<int>::iterator i;  // how do I initialize i?
mFieldInt.insert(i,10);

MField<int> stores a number of ints internally using a vector. Here's
the specification for MField -

http://www.h3dapi.org/uploads/api/H3DAPI_20_beta/docs/H3DAPI/html/cla...

My question though is, why does MField have an insert function
associated with it if its begin function returns a const_iterator? How
can one insert something into a MField object when the only function
that returns an iterator (instead of a const_iterator) is the insert
function itself. I'm totally confused by this.

How can I insert a value at the start of an MField object's internal
vector?

It seems as if the API of MField is somehow broken. As I'm not
familiar with the H3D API it cannot say how to fix it, but my guess is
that MField needs additional member functions MField::begin () and
MField::end () in order to be properly used (either those methods
should be added to the interface, or the insert method which takes
iterators should be deleted, so that MField provides read-only access
to its values).

However, such a question is not of interest in this newsgroup but
should be asked by the programmers of the H3D API (perhaps you should
post to forum of the H3D home page). All I can say is that the API
seems somehow inconsistent to me. But keep in mind that this is an
open project that lives on contributions, so (A) you shouldn't be
surprised to find some bugs, and (B) are free to contribute your own
bug fixes.

Regards,
Stuart
 
M

magnus.moraberg

It seems as if the API of MField is somehow broken. As I'm not
familiar with the H3D API it cannot say how to fix it, but my guess is
that MField needs additional member functions MField::begin () and
MField::end () in order to be properly used (either those methods
should be added to the interface, or the insert method which takes
iterators should be deleted, so that MField provides read-only access
to its values).

However, such a question is not of interest in this newsgroup but
should be asked by the programmers of the H3D API (perhaps you should
post to forum of the H3D home page). All I can say is that the API
seems somehow inconsistent to me. But keep in mind that this is an
open project that lives on contributions, so (A) you shouldn't be
surprised to find some bugs, and (B) are free to contribute your own
bug fixes.

Regards,
Stuart

Thanks for your reply. I'm quite new to C++ and the code didn't make
sense to me. Thanks for confirming my suspicions.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top