not like in java, everything is by default by reference. in c++, is by value. dreaming in java, but writing c++ gave me a nightmare yesterday night. the story is something like this:
class Family {
vector<Child> children;
vector<Child> getChildren() { return children; }
}
the calling code to the children in the family is by calling getChildren. then iterators are initialized like this:
vector<Child>::iterator pos = ourfamily.getChildren().begin();
vector<Child>::iterator end = ourfamily.getChildren().end();
while(pos != end) {
//print children
pos++;
}
another way to do this is like this way:
vector<Child>::iterator pos = ourfamily.getChildren().begin();
while (pos != ourfamily.getChildren().end()) {
//print children
pos++;
}
guess what? the two types of calling results in different print outs, not the orders, i mean the number of children. i thought i was wired at the beginning, is that a bug in the libstdc++ ? since, if the family is large enough, there'll be some segement fault. by checking everywhere, 3 hours passed.
in the morning, my post in some forum got a reply, the answer is :
vector<Child>& getChildren() { return children; }
because of being lack of this reference marker, the iterator pos and end are iterators of two different temporate objects !! and the second way to iterator even worse, which generate more than two temporate objects. segement fault maybe because some pointers in the iterator implementation pointed to some taken addresses. = -
DO REMEMBER: BY DEFAULT, CXX IS BY VALUE
this is really easy to be forgot if we wrote other languages for a long time. 3 hours just for a reference marker. orz...
class Family {
vector<Child> children;
vector<Child> getChildren() { return children; }
}
the calling code to the children in the family is by calling getChildren. then iterators are initialized like this:
vector<Child>::iterator pos = ourfamily.getChildren().begin();
vector<Child>::iterator end = ourfamily.getChildren().end();
while(pos != end) {
//print children
pos++;
}
another way to do this is like this way:
vector<Child>::iterator pos = ourfamily.getChildren().begin();
while (pos != ourfamily.getChildren().end()) {
//print children
pos++;
}
guess what? the two types of calling results in different print outs, not the orders, i mean the number of children. i thought i was wired at the beginning, is that a bug in the libstdc++ ? since, if the family is large enough, there'll be some segement fault. by checking everywhere, 3 hours passed.
in the morning, my post in some forum got a reply, the answer is :
vector<Child>& getChildren() { return children; }
because of being lack of this reference marker, the iterator pos and end are iterators of two different temporate objects !! and the second way to iterator even worse, which generate more than two temporate objects. segement fault maybe because some pointers in the iterator implementation pointed to some taken addresses. = -
DO REMEMBER: BY DEFAULT, CXX IS BY VALUE
this is really easy to be forgot if we wrote other languages for a long time. 3 hours just for a reference marker. orz...
No comments:
Post a Comment