473,413 Members | 1,778 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,413 software developers and data experts.

iterator to const object

Hi!

How can I declare iterator to const object ?

I've the following:

class ClassA : public list<ClassB>
{
.....
}

somefunction( const Class A *obj )
{
ClassA::iterator it; // <- this is wrong....

it = obj->begin();

....
}

regards
J.
Sep 22 '08 #1
3 2076
On 9ÔÂ22ÈÕ, ÏÂÎç5ʱ32·Ö, jarek <ja...@nospam.plwrote:
Hi!

How can I declare iterator to const object ?

I've the following:

class ClassA : public list<ClassB>
{
....

}

somefunction( const Class A *obj )
Guess it's ClassA
{
ClassA::iterator it; // <- this is wrong....

it = obj->begin();
actually it's wrong here,
as obj->begin() calls "const_iterator ClassA::begin() const"
so it returns a ClassA::const_iterator which is not convertible to
ClassA::iterator

More over, don't inherent from standard containers, preferring
composition
>
....

}

Sep 22 '08 #2
On Sep 22, 5:32*am, jarek <ja...@nospam.plwrote:
Hi!

How can I declare iterator to const object ?

I've the following:

class ClassA : public list<ClassB>
{
....

}

somefunction( const Class A *obj )
{
* * * * ClassA::iterator it; // <- this is wrong....

* * * * it = obj->begin();

* * * * ....

}

regards
J.
try ClassA::const_iterator const_it;

Sep 22 '08 #3
On Sep 22, 5:32 am, jarek <ja...@nospam.plwrote:
Hi!

How can I declare iterator to const object ?

I've the following:

class ClassA : public list<ClassB>
{
....

}

somefunction( const Class A *obj )
{
ClassA::iterator it; // <- this is wrong....

it = obj->begin();

....

}

regards
J.

1. deriving from containers is not usually the right thing to do, use
composition instead
2. use a reference_to_constant instead of a
mutable_pointer_to_constant

void foo( const A* obj ) { } // ptr can be reseated
void foo( const A* const obj ) { } // better
void foo( const A& obj ) { } // perfect

3. An iterator implies a type that can mutate its target. You can't
mutate a const target, so
const_iterator is required to obtain read only access.

Perhaps something like:

#include <iostream>
#include <list>

class B { };

class A
{
std::list< B m_list;

public:
typedef std::list< B >::const_iterator const_iterator;

const_iterator begin() const
{
return m_list.begin();
}
const_iterator end() const
{
return m_list.end();
}
void push_back( const B& r_b )
{
m_list.push_back(r_b);
}
std::size_t size() const
{
return m_list.size();
}
};

void somefunction( const A& r_a )
{
for( A::const_iterator it( r_a.begin() );
it != r_a.end();
++it )
{
std::cout << &(*it) << std::endl;
}
}

int main()
{
A a;
B b;
a.push_back(b);
a.push_back(b);
std::cout << "size: ";
std::cout << a.size() << std::endl;

somefunction( a );
}
Sep 23 '08 #4

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

Similar topics

0
by: sks_cpp | last post by:
I am trying to wrap the map iterator for keys and values. However, I seem to run into problems with the values for const_iterator - it works for all other combinations. Below I list my code and...
4
by: Scott Smedley | last post by:
Hi all, I'm trying to write a special adaptor iterator for my program. I have *almost* succeeded, though it fails under some circumstances. See the for-loop in main(). Any pointers/help...
3
by: CoolPint | last post by:
If I were to design my own container and its iterators, I understand that I need to provide both "iterator" and "const_iterator" so that begin() and end() return appropriate ones depending on the...
2
by: cppaddict | last post by:
My initial method sig was: bool NativeOcr::testPoints(Point positionCoords, std::map<Point, COLORREF> testPnts) const ; I changed it to: bool NativeOcr::testPoints(const Point&...
7
by: Prawit Chaivong | last post by:
Hi, gurus Is it safe to do this in function? 'return &(*iterator)'; And iterator is std::set<something>::iterator Regards,
6
by: PengYu.UT | last post by:
Hi, Suppose I have a list which contains pointers. I want the pointer got by dereferencing the iterator be a pointer pointing to a const object. But std::list<const T*>::const_iterator doens't...
3
by: toton | last post by:
Hi, I want ro iterate over a few container class within a range specified, instead of begin & end. How to construct a range class, which takes start & end, and iteration is available within that...
0
by: mailforpr | last post by:
Hi. Let me introduce an iterator to you, the so-called "Abstract Iterator" I developed the other day. I actually have no idea if there's another "Abstract Iterator" out there, as I have never...
3
by: Belebele | last post by:
I have an Element class which is abstract and I would like to have an object of the Iterator class to iterate over a range of elements. I would like to use std::for_each to instrument the...
7
by: Jess | last post by:
Hello, I learned that if we do "v.end()", then the returned iterator is a temporary object and hence cannot be changed like --v.end(); Why is the returned iterator a temporary pointer? I...
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: 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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.