RE: [Dev-C++] STL, iterators and pointers?
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Ioannis V. <no...@ya...> - 2001-07-07 13:14:27
|
> -----Original Message-----
> From: dev...@li...
> [mailto:dev...@li...] On Behalf
> Of Phil Schulz
> Sent: Saturday, July 07, 2001 10:21 AM
> To: dev...@li...
> Subject: Re: [Dev-C++] STL, iterators and pointers?
>
> okay, that's pretty clear. I had been looking at examples
> that start with defining an empty container and then adding
> to it using pushback() or insert(), so it wasn't clear to
> me... I forgot that I probably have to define copy
> constructors for my classes. Haven't tested this yet, but
> this might be the problem...
When you use pushback() and vector is full, new objects within the
vector are created.
> nope, definitely defined as objects.
> My problem is when I try to assign &(*iter) to a pointer, I
> get compile errors
> "initialization to `Book *' from `const Book *' discards
> qualifiers" I'll make the new pointer a const, but that'll
> require another function to be declared a CONST and on and on
> , to a point of uselessness.
Code that works:
A)
#include <vector>
class A
{
};
int main()
{
vector<A> test(10);
A *p;
p=&test[0];
}
B)
#include <vector>
class A
{
};
int main()
{
vector<A> test(10);
vector<A>::iterator iter=test.begin();
A *p;
p=&(*iter);
}
Code that does not work (of course):
#include <vector>
class A
{
};
int main()
{
vector<A> test(10);
vector<A>::const_iterator iter=test.begin();
A *p;
p=&(*iter);
}
You probably were using const_iterator or doing something else wrong.
Ioannis
* Ioannis Vranos
* Programming pages: http://www.noicys.f2s.com
* Alternative URL: http://run.to/noicys
|