|
5.2.5 信息获取和统计 中提到仿函数Class SimpleFunctor
{
public:
void operator()(int &i)
{
.....
}
};
和如下函数有同样的被调用的方式
SimpleFuncotr(int &i)
{
...
}
疑问1
由于成员函数有隐藏参数SimpleFunctor* this,成员函数和普通函数还是有区别的
疑问2
很长,先贴书中代码
- #include <osg\io_utils>
- #include <osg\TriangleFunctor>
- #include <osg\Drawable>
- #include <osgDB\ReadFile>
- #include <iostream>
- class AttributePrinter : public osg::Drawable::AttributeFunctor
- {
- public :
- typedef osg::Drawable::AttributeType AttributeType;
- inline const char * getTypeName(AttributeType type)
- {
- static const char* typeNames[]=
- {
- "Vertices","Weights","Normals","Colors","Secondary Colors",
- "Fog Coords","Attribute6","Attribute7",
- "Texture Coords 0","Texture Coords 1","Texture Coords 2",
- "Texture Coords 3","Texture Coords 4","Texture Coords 5",
- "Texture Coords 6","Texture Coords 7"
- };
- return typeNames[type];
- }
- template < typename T>
- void printInfo(AttributeType type, unsigned int size, T* front)
- {
- std::cout << "***" << getTypeName(type) <<":" << size <<std::endl;
- for (unsigned int i=0; i<size ;i++)
- {
- std::cout << "(" << *(front + 1) << ");" ;
- }
- std::cout <<std::endl<< std::endl;
- }
- virtual void apply(AttributeType type ,unsigned int size ,float* front)
- {
- printInfo(type,size,front);
- }
- virtual void apply(AttributeType type ,unsigned int size ,osg::Vec2* front)
- {
- printInfo(type,size,front);
- }
- virtual void apply(AttributeType type ,unsigned int size ,osg::Vec3* front)
- {
- printInfo(type,size,front);
- }
- virtual void apply(AttributeType type ,unsigned int size ,osg::Vec4* front)
- {
- printInfo(type,size,front);
- }
- };
- struct TrianglePrinter
- {
- TrianglePrinter()
- {
- std::cout<< "****Triangles***" << std::endl;
- }
- void operator()(const osg::Vec3 & v1,const osg::Vec3 & v2,
- const osg::Vec3 & v3,bool) const
- {
- std::cout << "(" << v1 << ");" << "(" << v2 << ");" << "(" << v3 << ")" << std::endl;
- }
- };
- class FindGeometryVisitor: public osg::NodeVisitor
- {
- public:
- FindGeometryVisitor()
- :osg::NodeVisitor(TRAVERSE_ALL_CHILDREN){}
- virtual void apply(osg::Node &node)
- {
- traverse(node);
- }
- virtual void apply(osg::Geode &node)
- {
- for (unsigned int i=0; i<node.getNumDrawables(); i++)
- {
- osg::Drawable* drawable = node.getDrawable(i);
- if (!drawable)
- {
- continue;
- }
- std::cout << "[" << drawable->libraryName()
- << "::" << drawable->className() << "]" <<std::endl;
- AttributePrinter attrPrinter;
- drawable->accept(attrPrinter);
- // 这里就是疑问产生处
- osg::TriangleFunctor<TrianglePrinter> triPrinter;
- drawable->accept(triPrinter);
- std::cout<< std::endl;
- }
- traverse(node);
- }
- };
- int main(int argc, char** argv)
- {
- osg::ArgumentParser arguments(&argc, argv);
- osg::Node * model = osgDB::readNodeFiles(arguments);
- if (!model)
- {
- model = osgDB::readNodeFile("exported.ive");
- }
- FindGeometryVisitor fgv;
- if (model)
- {
- model->accept(fgv);
- }
- getchar();
- return 0;
- }
复制代码
osg::TriangleFunctor<TrianglePrinter> triPrinter;
drawable->accept(triPrinter);
运行后,triPrinter确实被输出了,也就是重载()被顺利调用,
要达到这个效果我理解应该满足:
1,TriangleFunctor实现了apply()方法
2,在被实现的apply()方法中有一个和TrignlePrinter的操作符()同样参数格式的函数;
通过看源码,1的出处找到了。但是2没有找到。
疑问2:
被重载的()是何时被调用的? |
|