我有一组要流出的 SceneElement。这是聚合类:
class scene{
public:
vector<sceneElement> elements;
void addElement(sceneElement);
void toStream(std::ostream &);
void fromStream(std::istream &);
};
void scene::addElement(sceneElement e){
elements.insert(elements.end(), e);
}
void scene::toStream(std::ostream &strm){
strm << SCENE_PRE;
int i;
for(i=0; i<elements.size(); i++){
elements[i].toStream(strm);
}
strm << SCENE_POST;
}
这是基类:
class sceneElement{
public:
virtual void toStream(std::ostream &);
virtual void fromStream(std::istream &);
};
void sceneElement::toStream(std::ostream &str){
str << "SCENE ELEMENT";
}
void sceneElement::fromStream(std::istream &){
std::cerr << "this is not supposed to be called";
}
这是派生类之一:
class camera : public sceneElement{
public:
P3d location;
P3d direction;
double fov;
int toString(char**);
virtual void toStream(std::ostream &);
virtual void fromStream(std::istream &);
};
void camera::toStream(std::ostream &strm){
strm << CAMERA_PRE << TAG_LOCATION;
location.toStream(strm);
strm << TAG_DIRECTION;
direction.toStream(strm);
strm << TAG_FOV << fov << CAMERA_POST;
}
但是当我运行这段代码时:
scene sc;
sc.addElement(s);
sc.toStream(cout);
它显示
<_SCN>SCENE ELEMENT<SCN_>
而不是它应该的实际元素。