I recall reading somewhere, can't recall where though, that operator-> is transient. That it will look through objects with operator-> until it find something that isn't a pointer and then running a normal operator. on that. I have however run into a problem with this,
Consider this code:
#include <boost/shared_ptr.hpp>
#include <vector>
#include <iostream>
struct Foo {
Foo(int val) : i(val) {}
typedef boost::shared_ptr<Foo> ptr;
int i;
};
int main(int argc, char** argv) {
typedef std::vector<Foo::ptr> FooVec;
FooVec v;
for(FooVec::iterator it = v.begin(); it != v.end(); ++it) {
std::cout <<it->i <<std::endl;
}
return 0;
}
I get this error:
ptr.cpp: In function ‘int main(int, char**)’:
ptr.cpp:15:19: error: ‘class boost::shared_ptr<Foo>’ has no member named ‘i’
std::cout <<it->i <<std::endl;
Since a boost::shared_ptr isn't a pointer. I could solve this by instead writing
std::cout <<(*it)->i <<std::endl
But I think that looks a lot worse, I tried std::shared_ptr as well but that got the same problem. Is there a nice way of solving this or am I stuck with (*it)->i
?
I recall reading somewhere, can't recall where though, that operator-> is transient.
Not in C++. You'll need to double-dereference as you already discovered.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.