By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,808 Members | 1,477 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,808 IT Pros & Developers. It's quick & easy.

reverse_iterator and const_reverse_iterator

P: n/a
Hello,

I think the two reverse_iterators are the same, except that the const_
version doesn't allow me to change the value pointed to by the
iterators. However, I have a program that works for reverse_iterator
but not const_reverse_iterator:

for(vector<int>::const_reverse_iterator i = v.rbegin(); i != v.rend();
++i)
cout << (*i) << endl;

This one fails, with error

no match for 'operator!=' in 'i != std::vector<_Tp, _Alloc>::rend()
[with _Tp = int, _Alloc = std::allocator<int>]()'

If I replace const_reverse_iterator with reverse_iterator, then
everything works fine. Is there some subtle difference between the
two iterators?

Thanks,
Jess

Jun 17 '07 #1
Share this Question
Share on Google+
20 Replies


P: n/a
On Sun, 17 Jun 2007 06:45:56 -0700, Jess wrote:
>I think the two reverse_iterators are the same, except that the const_
version doesn't allow me to change the value pointed to by the
iterators. However, I have a program that works for reverse_iterator
but not const_reverse_iterator:

for(vector<int>::const_reverse_iterator i = v.rbegin(); i != v.rend();
++i)
cout << (*i) << endl;

This one fails, with error

no match for 'operator!=' in 'i != std::vector<_Tp, _Alloc>::rend()
[with _Tp = int, _Alloc = std::allocator<int>]()'
I guess your compiler cannot distinguish between the const and the
non-const rend(). Try:

for(vector<int>::const_reverse_iterator i = v.rbegin();
i != ((const std::vector<int>) v).rend();
++i)
--
Roland Pibinger
"The best software is simple, elegant, and full of drama" - Grady Booch
Jun 17 '07 #2

P: n/a
On Sun, 17 Jun 2007 06:45:56 -0700, Jess wrote:
>If I replace const_reverse_iterator with reverse_iterator, then
everything works fine. Is there some subtle difference between the
two iterators?
Yes. Basicly these are two different types/classes.

>However, I have a program that works for reverse_iterator
but not const_reverse_iterator:
>for(vector<int>::const_reverse_iterator i = v.rbegin(); i != v.rend();
++i)
cout << (*i) << endl;

There are two overloaded rend(void) functions, that differ in their
return types. Now C++ does NOT permit function overloading by return
value. So the function "const_reverse_iterator rend() const" could be
applied on const objects only, but not on non constant ones.
int arr[] = {1, 2, 3};
const vector<intv(arr, arr+3);

typedef vector<int>::const_reverse_iterator ViCRI;
for(ViCRI i = v.rbegin(); i!=v.rend(); ++i)
cout << (*i) << endl;
And the function "reverse_iterator rend()" can be applied on non
constant objects only.

One way get around it is to explicitly cast the result returned by
rend(). And here is a different way deal with this:
int arr[] = {1, 2, 3};
const vector<intv(arr, arr+3);

typedef vector<int>::const_reverse_iterator ViCRI;
for(ViCRI i = v.rbegin(), End = v.end(); i!=End; ++i)
cout << (*i) << endl;
Jun 17 '07 #3

P: n/a
Roland Pibinger wrote:
On Sun, 17 Jun 2007 06:45:56 -0700, Jess wrote:
>I think the two reverse_iterators are the same, except that the const_
version doesn't allow me to change the value pointed to by the
iterators. However, I have a program that works for reverse_iterator
but not const_reverse_iterator:

for(vector<int>::const_reverse_iterator i = v.rbegin(); i != v.rend();
++i)
cout << (*i) << endl;

This one fails, with error

no match for 'operator!=' in 'i != std::vector<_Tp, _Alloc>::rend()
[with _Tp = int, _Alloc = std::allocator<int>]()'

I guess your compiler cannot distinguish between the const and the
non-const rend(). Try:
Seems like it does distinguish between them, and calls the non-const
version on a non-const vector. The problem arises from comparing a
const_iterator to a plain iterator.
for(vector<int>::const_reverse_iterator i = v.rbegin();
i != ((const std::vector<int>) v).rend();
i != ((const std::vector<int>&) v).rend();

That's a minor typo, but critical. As written, it copies the vector,
creating an iterator that points into the copy rather than the original.

A simpler way of doing this, if you have a library that conforms to the
not-yet-official C++0x standard, is to call crbegin() and crend()
instead of rbegin() and rend(). crbegin() and crend() return const
iterators.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Jun 17 '07 #4

P: n/a
On Jun 18, 4:17 am, Pete Becker <p...@versatilecoding.comwrote:
Roland Pibinger wrote:
On Sun, 17 Jun 2007 06:45:56 -0700, Jess wrote:
I think the two reverse_iterators are the same, except that the const_
version doesn't allow me to change the value pointed to by the
iterators. However, I have a program that works for reverse_iterator
but not const_reverse_iterator:
for(vector<int>::const_reverse_iterator i = v.rbegin(); i != v.rend();
++i)
cout << (*i) << endl;
This one fails, with error
no match for 'operator!=' in 'i != std::vector<_Tp, _Alloc>::rend()
[with _Tp = int, _Alloc = std::allocator<int>]()'
I guess your compiler cannot distinguish between the const and the
non-const rend(). Try:

Seems like it does distinguish between them, and calls the non-const
version on a non-const vector. The problem arises from comparing a
const_iterator to a plain iterator.
for(vector<int>::const_reverse_iterator i = v.rbegin();
i != ((const std::vector<int>) v).rend();

i != ((const std::vector<int>&) v).rend();

That's a minor typo, but critical. As written, it copies the vector,
creating an iterator that points into the copy rather than the original.
Thanks, I tried both methods, but neither worked... I think I only
need to cast a non-const "v" to a const "v", hence I tried

for(vector<int>::const_reverse_iterator i = v.rbegin(); i !=
(static_cast<const vector<int)v.rend(); ++i)
cout << (*i) << endl;

Unfortunately, it failed as well...Moreover, what's the difference
between casting to a class type and casting to a reference?

Jess

Jun 17 '07 #5

P: n/a
On Jun 18, 4:10 am, "V. R. Marinov" <v.r.mari...@gmail.comwrote:
On Sun, 17 Jun 2007 06:45:56 -0700, Jess wrote:
>If I replace const_reverse_iterator with reverse_iterator, then
>everything works fine. Is there some subtle difference between the
>two iterators?

Yes. Basicly these are two different types/classes.
>However, I have a program that works for reverse_iterator
>but not const_reverse_iterator:
>for(vector<int>::const_reverse_iterator i = v.rbegin(); i != v.rend();
>++i)
cout << (*i) << endl;

There are two overloaded rend(void) functions, that differ in their
return types. Now C++ does NOT permit function overloading by return
value. So the function "const_reverse_iterator rend() const" could be
applied on const objects only, but not on non constant ones.

int arr[] = {1, 2, 3};
const vector<intv(arr, arr+3);

typedef vector<int>::const_reverse_iterator ViCRI;
for(ViCRI i = v.rbegin(); i!=v.rend(); ++i)
cout << (*i) << endl;

And the function "reverse_iterator rend()" can be applied on non
constant objects only.

One way get around it is to explicitly cast the result returned by
rend(). And here is a different way deal with this:

int arr[] = {1, 2, 3};
const vector<intv(arr, arr+3);

typedef vector<int>::const_reverse_iterator ViCRI;
for(ViCRI i = v.rbegin(), End = v.end(); i!=End; ++i)
cout << (*i) << endl;

Thanks. By "explicitly cast the result returned by rend()", do you
mean I should cast const away? Is it done by using "v.end()" instead
of "rend()"? However, why do we need to cast away constness if "v" is
a const vector?

Jess

Jun 17 '07 #6

P: n/a
Jess написа:
On Jun 18, 4:10 am, "V. R. Marinov" <v.r.mari...@gmail.comwrote:
>On Sun, 17 Jun 2007 06:45:56 -0700, Jess wrote:
> >If I replace const_reverse_iterator with reverse_iterator, then
everything works fine. Is there some subtle difference between the
two iterators?

Yes. Basicly these are two different types/classes.
> >However, I have a program that works for reverse_iterator
but not const_reverse_iterator:
> >for(vector<int>::const_reverse_iterator i = v.rbegin(); i != v.rend();
++i)
cout << (*i) << endl;

There are two overloaded rend(void) functions, that differ in their
return types. Now C++ does NOT permit function overloading by return
value. So the function "const_reverse_iterator rend() const" could be
applied on const objects only, but not on non constant ones.

int arr[] = {1, 2, 3};
const vector<intv(arr, arr+3);

typedef vector<int>::const_reverse_iterator ViCRI;
for(ViCRI i = v.rbegin(); i!=v.rend(); ++i)
cout << (*i) << endl;

And the function "reverse_iterator rend()" can be applied on non
constant objects only.

One way get around it is to explicitly cast the result returned by
rend(). And here is a different way deal with this:

int arr[] = {1, 2, 3};
const vector<intv(arr, arr+3);

typedef vector<int>::const_reverse_iterator ViCRI;
for(ViCRI i = v.rbegin(), End = v.end(); i!=End; ++i)
cout << (*i) << endl;


Thanks. By "explicitly cast the result returned by rend()", do you
mean I should cast const away? Is it done by using "v.end()" instead
of "rend()"? However, why do we need to cast away constness if "v" is
a const vector?
No. I was referring to the non-const case.

/********************** CODE **********************/
int arr[] = {1, 2, 3};
vector<intv(arr, arr+3);

typedef vector<int>::const_reverse_iterator ViCRI;

for(ViCRI i = v.rbegin(); i!=ViCRI(v.rend()); ++i)
cout << (*i) << endl;
/********************** CODE **********************/
Jun 18 '07 #7

P: n/a

Jess :
On Jun 18, 4:17 am, Pete Becker <p...@versatilecoding.comwrote:
Roland Pibinger wrote:
On Sun, 17 Jun 2007 06:45:56 -0700, Jess wrote:
>I think the two reverse_iterators are the same, except that the const_
>version doesn't allow me to change the value pointed to by the
>iterators. However, I have a program that works for reverse_iterator
>but not const_reverse_iterator:
>for(vector<int>::const_reverse_iterator i = v.rbegin(); i != v.rend();
>++i)
> cout << (*i) << endl;
>This one fails, with error
>no match for 'operator!=' in 'i != std::vector<_Tp, _Alloc>::rend()
>[with _Tp = int, _Alloc = std::allocator<int>]()'
I guess your compiler cannot distinguish between the const and the
non-const rend(). Try:
Seems like it does distinguish between them, and calls the non-const
version on a non-const vector. The problem arises from comparing a
const_iterator to a plain iterator.
for(vector<int>::const_reverse_iterator i = v.rbegin();
i != ((const std::vector<int>) v).rend();
i != ((const std::vector<int>&) v).rend();

That's a minor typo, but critical. As written, it copies the vector,
creating an iterator that points into the copy rather than the original.

Thanks, I tried both methods, but neither worked... I think I only
need to cast a non-const "v" to a const "v", hence I tried

for(vector<int>::const_reverse_iterator i = v.rbegin(); i !=
(static_cast<const vector<int)v.rend(); ++i)
cout << (*i) << endl;

Unfortunately, it failed as well
It looks like you are using the wrong syntax. It should be.

i != static_cast<const vector<int>& >(v).rend()

>...Moreover, what's the difference
between casting to a class type and casting to a reference?
Please reread Pete Becker's post.

Jun 18 '07 #8

P: n/a
On Sun, 17 Jun 2007 15:48:43 -0700, Jess wrote:
>Thanks, I tried both methods, but neither worked... I think I only
need to cast a non-const "v" to a const "v", hence I tried

for(vector<int>::const_reverse_iterator i = v.rbegin(); i !=
(static_cast<const vector<int)v.rend(); ++i)
cout << (*i) << endl;
Actually, the cast is a bad idea anyway (I used it only to find the
cause of the problem). Better use a reference to const vector on which
only const member functions can be invoked:

vector<intv;
// ...
const vector<int>& cv = v; // reference
for(vector<int>::const_reverse_iterator i = cv.rbegin();
i != cv.rend(); ++i)
cout << (*i) << endl;

--
Roland Pibinger
"The best software is simple, elegant, and full of drama" - Grady Booch
Jun 18 '07 #9

P: n/a
On Jun 18, 2:35 pm, "V.R. Marinov" <v.r.mari...@gmail.comwrote:
Jess :
On Jun 18, 4:17 am, Pete Becker <p...@versatilecoding.comwrote:
Roland Pibinger wrote:
On Sun, 17 Jun 2007 06:45:56 -0700, Jess wrote:
I think the two reverse_iterators are the same, except that the const_
version doesn't allow me to change the value pointed to by the
iterators. However, I have a program that works for reverse_iterator
but not const_reverse_iterator:
for(vector<int>::const_reverse_iterator i = v.rbegin(); i != v.rend();
++i)
cout << (*i) << endl;
This one fails, with error
no match for 'operator!=' in 'i != std::vector<_Tp, _Alloc>::rend()
[with _Tp = int, _Alloc = std::allocator<int>]()'
I guess your compiler cannot distinguish between the const and the
non-const rend(). Try:
Seems like it does distinguish between them, and calls the non-const
version on a non-const vector. The problem arises from comparing a
const_iterator to a plain iterator.
for(vector<int>::const_reverse_iterator i = v.rbegin();
i != ((const std::vector<int>) v).rend();
i != ((const std::vector<int>&) v).rend();
That's a minor typo, but critical. As written, it copies the vector,
creating an iterator that points into the copy rather than the original.
Thanks, I tried both methods, but neither worked... I think I only
need to cast a non-const "v" to a const "v", hence I tried
for(vector<int>::const_reverse_iterator i = v.rbegin(); i !=
(static_cast<const vector<int)v.rend(); ++i)
cout << (*i) << endl;
Unfortunately, it failed as well

It looks like you are using the wrong syntax. It should be.

i != static_cast<const vector<int>& >(v).rend()
Thanks. I tried but it still doesn't work. Here's the entire
program.

#include<vector>
#include<string>
#include<iterator>
#include<iostream>
using namespace std;

int main(){
vector<intv;
for(int i = 0; i != 10; ++i)
v.push_back(i);

for(vector<int>::const_reverse_iterator i = v.rbegin(); i !=
static_cast<vector<int>& >(v).rend(); ++i)
cout << (*i) << endl;
return 0;
}

The error message was

no match for 'operator!=' in 'i != std::vector<_Tp, _Alloc>::rend()
[with _Tp = int, _Alloc = std::allocator<int>]()'

Jess

Jun 18 '07 #10

P: n/a
On Jun 18, 4:17 am, Pete Becker <p...@versatilecoding.comwrote:
Roland Pibinger wrote:
On Sun, 17 Jun 2007 06:45:56 -0700, Jess wrote:
I think the two reverse_iterators are the same, except that the const_
version doesn't allow me to change the value pointed to by the
iterators. However, I have a program that works for reverse_iterator
but not const_reverse_iterator:
for(vector<int>::const_reverse_iterator i = v.rbegin(); i != v.rend();
++i)
cout << (*i) << endl;
This one fails, with error
no match for 'operator!=' in 'i != std::vector<_Tp, _Alloc>::rend()
[with _Tp = int, _Alloc = std::allocator<int>]()'
I guess your compiler cannot distinguish between the const and the
non-const rend(). Try:

Seems like it does distinguish between them, and calls the non-const
version on a non-const vector. The problem arises from comparing a
const_iterator to a plain iterator.
for(vector<int>::const_reverse_iterator i = v.rbegin();
i != ((const std::vector<int>) v).rend();

i != ((const std::vector<int>&) v).rend();

That's a minor typo, but critical. As written, it copies the vector,
creating an iterator that points into the copy rather than the original.
Thanks Peter, but I don't quite understand this.. Do you mean that if
I don't use the reference, then the program copies the vector and
create an iterator pointing to the copy rather than the original?
Does it happen to all the casting operations? In otherwords, whenever
I do a cast on an object "x" (static_cast or other cast), I can't
modify "x" through the result of the cast? Why is there such
constraint?

Jess

Jun 18 '07 #11

P: n/a
On Jun 18, 5:41 pm, rpbg...@yahoo.com (Roland Pibinger) wrote:
On Sun, 17 Jun 2007 15:48:43 -0700, Jess wrote:
Thanks, I tried both methods, but neither worked... I think I only
need to cast a non-const "v" to a const "v", hence I tried
for(vector<int>::const_reverse_iterator i = v.rbegin(); i !=
(static_cast<const vector<int)v.rend(); ++i)
cout << (*i) << endl;

Actually, the cast is a bad idea anyway (I used it only to find the
cause of the problem). Better use a reference to const vector on which
only const member functions can be invoked:

vector<intv;
// ...
const vector<int>& cv = v; // reference
for(vector<int>::const_reverse_iterator i = cv.rbegin();
i != cv.rend(); ++i)
cout << (*i) << endl;
Thanks, this is a very good idea. I'll try to avoid casting. :) To
me, it's quite surprising that I can't use a const function on a non-
const object. The const function promises not to change the object,
so what's wrong with applying the const function on a non-const
object? the function doesn't do any harm to the non-const object.
It's clear why we shouldn't use a non-const function on a const
object, but not so clear the other way round...

Jess

Jun 18 '07 #12

P: n/a
Jess wrote:
>
To
me, it's quite surprising that I can't use a const function on a non-
const object. The const function promises not to change the object,
so what's wrong with applying the const function on a non-const
object?
There's nothing wrong with it, but when you have a non-const object and
you call a function that's overloaded for const and non-const, you get
the non-const version.

struct S
{
void mf();
void mf() const;
};

void f(S& s, const S& cs)
{
s.mf(); // calls S::mf()
cs.mf(); // calls S::mf() const
((const S&)s).mf(); // calls S::mf() const
}

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Jun 18 '07 #13

P: n/a

Jess <wd***@hotmail.comwrote in message...
On Jun 18, 2:35 pm, "V.R. Marinov" <v.r.mari...@gmail.comwrote:

It looks like you are using the wrong syntax. It should be.

i != static_cast<const vector<int>& >(v).rend()

Thanks. I tried but it still doesn't work. Here's the entire
program.

#include<vector>
#include<string>
#include<iterator>
#include<iostream>
// using namespace std; // Yuck
>
int main(){
using std::vector;
using std::cout;
using std::endl;
vector<intv;
for(int i = 0; i != 10; ++i) v.push_back(i);

for(vector<int>::const_reverse_iterator i = v.rbegin(); i !=
static_cast<vector<int>& >(v).rend(); ++i)
Did you try it with the 'const', as suggested?
i != static_cast<const vector<int>& >(v).rend(); ++i)
cout << (*i) << endl;
return 0;
}

The error message was
no match for 'operator!=' in 'i != std::vector<_Tp, _Alloc>::rend()
[with _Tp = int, _Alloc = std::allocator<int>]()'
Jess
--
Bob R
POVrookie
Jun 18 '07 #14

P: n/a
On Jun 18, 10:23 pm, Pete Becker <p...@versatilecoding.comwrote:
Jess wrote:
To
me, it's quite surprising that I can't use a const function on a non-
const object. The const function promises not to change the object,
so what's wrong with applying the const function on a non-const
object?

There's nothing wrong with it, but when you have a non-const object and
you call a function that's overloaded for const and non-const, you get
the non-const version.

struct S
{
void mf();
void mf() const;

};

void f(S& s, const S& cs)
{
s.mf(); // calls S::mf()
cs.mf(); // calls S::mf() const
((const S&)s).mf(); // calls S::mf() const

}

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)

Thanks for clarifying this. Would you please also tell me if it is
true that whenever I do a casting (static_cast or other cast) on an
object, I have to cast to a reference type if I need the object itself
to be changed? If I don't cast the object to a reference type, then I
get a temporary object? I guess internally, if I cast to a reference
type, then no constructor is called, but if I don't cast to a
reference type, then a copy constructor is called...

Jess

Jun 18 '07 #15

P: n/a
On Jun 19, 3:33 am, "BobR" <removeBadB...@worldnet.att.netwrote:
Jess <w...@hotmail.comwrote in message...
On Jun 18, 2:35 pm, "V.R. Marinov" <v.r.mari...@gmail.comwrote:
It looks like you are using the wrong syntax. It should be.
i != static_cast<const vector<int>& >(v).rend()
Thanks. I tried but it still doesn't work. Here's the entire
program.
#include<vector>
#include<string>
#include<iterator>
#include<iostream>

// using namespace std; // Yuck
I know this opens up the entire std namespace, but is it still bad if
I use it in my .cpp file?
int main(){

using std::vector;
using std::cout;
using std::endl;
vector<intv;
for(int i = 0; i != 10; ++i) v.push_back(i);
for(vector<int>::const_reverse_iterator i = v.rbegin(); i !=
static_cast<vector<int>& >(v).rend(); ++i)

Did you try it with the 'const', as suggested?
Ah, I forgot the "const in static_cast! Thanks a lot! :)

Jess

Jun 18 '07 #16

P: n/a

Jess wrote in message...
On Jun 19, 3:33 am, "BobR" wrote:

// using namespace std; // Yuck

I know this opens up the entire std namespace, but is it still bad if
I use it in my .cpp file?
I won't say it's 'bad', just not good. <G>
Never put that line in a header, that is bad.
int main(){
The 'better' way:
using std::vector;
using std::cout;
using std::endl;
The 'best' way:

std::vector<intvint;

Unless you have a boss dictating the format, it's a style thing - your
choice.
Don't change boats in mid-stream, try to be consistant throughout your
code/project.
>
Ah, I forgot the "const in static_cast! Thanks a lot! :)
Jess
You're welcome. I assume you "Got 'er done."?

--
Bob R
POVrookie
Jun 19 '07 #17

P: n/a
On Jun 19, 12:59 am, Jess <w...@hotmail.comwrote:
On Jun 18, 10:23 pm, Pete Becker <p...@versatilecoding.comwrote:
Thanks for clarifying this. Would you please also tell me if it is
true that whenever I do a casting (static_cast or other cast) on an
object, I have to cast to a reference type if I need the object itself
to be changed? If I don't cast the object to a reference type, then I
get a temporary object? I guess internally, if I cast to a reference
type, then no constructor is called, but if I don't cast to a
reference type, then a copy constructor is called...
Basically, yes, but don't forget that pointers are objects. The
basic rule is that the result is a reference, and the expression
to be converted is an lvalue with a compatible type, there will
be no new object; in all other cases, the result of a cast is a
new object of the target type.

As I said, however, don't forget that pointers are objects. In
something like "static_cast< Base* >( ptrToDerived )", the new
object is the Base*, which however points to the Base part of
whatever object ptrToDerived points to. The only new object is
the pointer.

--
James Kanze (GABI Software, from CAI) email:ja*********@gmail.com
Conseils en informatique oriente objet/
Beratung in objektorientierter Datenverarbeitung
9 place Smard, 78210 St.-Cyr-l'cole, France, +33 (0)1 30 23 00 34

Jun 19 '07 #18

P: n/a
On Jun 19, 7:37 pm, James Kanze <james.ka...@gmail.comwrote:
On Jun 19, 12:59 am, Jess <w...@hotmail.comwrote:
On Jun 18, 10:23 pm, Pete Becker <p...@versatilecoding.comwrote:
Thanks for clarifying this. Would you please also tell me if it is
true that whenever I do a casting (static_cast or other cast) on an
object, I have to cast to a reference type if I need the object itself
to be changed? If I don't cast the object to a reference type, then I
get a temporary object? I guess internally, if I cast to a reference
type, then no constructor is called, but if I don't cast to a
reference type, then a copy constructor is called...

Basically, yes, but don't forget that pointers are objects. The
basic rule is that the result is a reference, and the expression
to be converted is an lvalue with a compatible type, there will
be no new object; in all other cases, the result of a cast is a
new object of the target type.

As I said, however, don't forget that pointers are objects. In
something like "static_cast< Base* >( ptrToDerived )", the new
object is the Base*, which however points to the Base part of
whatever object ptrToDerived points to. The only new object is
the pointer.
Thanks. So if I cast an object to a pointer type or other class type,
but not ref-type, then new an object is always created; in the case of
the pointer,the new object is a pointer that has the same value as the
old pointer hence they point to the same object and we can modify the
object through the new pointer. If I cast the object to a ref-type,
then there is no object created at all, not even the pointer, is this
right?

Thanks,
Jess

Jun 19 '07 #19

P: n/a
In article <46*************@news.utanet.at>, rp*****@yahoo.com says...
On Sun, 17 Jun 2007 15:48:43 -0700, Jess wrote:
Thanks, I tried both methods, but neither worked... I think I only
need to cast a non-const "v" to a const "v", hence I tried

for(vector<int>::const_reverse_iterator i = v.rbegin(); i !=
(static_cast<const vector<int)v.rend(); ++i)
cout << (*i) << endl;

Actually, the cast is a bad idea anyway (I used it only to find the
cause of the problem). Better use a reference to const vector on which
only const member functions can be invoked:

vector<intv;
// ...
const vector<int>& cv = v; // reference
for(vector<int>::const_reverse_iterator i = cv.rbegin();
i != cv.rend(); ++i)
cout << (*i) << endl;
Of course if that's what you're doing, you really just want:

std::copy(v.rbegin(), v.rend(),
std::ostream_iterator<int>(std::cout, "\n"));

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jun 21 '07 #20

P: n/a
On Jun 19, 1:07 pm, Jess <w...@hotmail.comwrote:
On Jun 19, 7:37 pm, James Kanze <james.ka...@gmail.comwrote:
On Jun 19, 12:59 am, Jess <w...@hotmail.comwrote:
On Jun 18, 10:23 pm, Pete Becker <p...@versatilecoding.comwrote:
Thanks for clarifying this. Would you please also tell me if it is
true that whenever I do a casting (static_cast or other cast) on an
object, I have to cast to a reference type if I need the object itself
to be changed? If I don't cast the object to a reference type, then I
get a temporary object? I guess internally, if I cast to a reference
type, then no constructor is called, but if I don't cast to a
reference type, then a copy constructor is called...
Basically, yes, but don't forget that pointers are objects. The
basic rule is that the result is a reference, and the expression
to be converted is an lvalue with a compatible type, there will
be no new object; in all other cases, the result of a cast is a
new object of the target type.
As I said, however, don't forget that pointers are objects. In
something like "static_cast< Base* >( ptrToDerived )", the new
object is the Base*, which however points to the Base part of
whatever object ptrToDerived points to. The only new object is
the pointer.
Thanks. So if I cast an object to a pointer type or other class type,
but not ref-type, then new an object is always created; in the case of
the pointer,the new object is a pointer that has the same value as the
old pointer hence they point to the same object and we can modify the
object through the new pointer. If I cast the object to a ref-type,
then there is no object created at all, not even the pointer, is this
right?
Almost. The case of casting to a reference type is a bit more
subtle; basically, "static_cast<T&>(v)" works exactly like: "T&
ref( v )", but without the name "ref". If the value being
converted is an lvalue, and reference compatible (i.e. the type
can somehow be directly bound to the reference), then there is
no new object. Otherwise, the reference must be to const, and
you may very well get a new object.

--
James Kanze (GABI Software, from CAI) email:ja*********@gmail.com
Conseils en informatique oriente objet/
Beratung in objektorientierter Datenverarbeitung
9 place Smard, 78210 St.-Cyr-l'cole, France, +33 (0)1 30 23 00 34

Jun 21 '07 #21

This discussion thread is closed

Replies have been disabled for this discussion.