0

我有一个名为 Test 的类,它继承了 TopoDS_Face 类。已经从这个问题中得到了一些提示,但是......

    // Test.h
    class Test : public TopoDS_Face
    {
      public:
         void operator = (const TopoDS_Face& base_)
         {
            TopoDS_Face::operator=(base_);
         }
    }

    // testmain.cpp 
    ...
    int main() {
      //extract faces from IGES face
      for (int i = 1; i <= nbs; i++) {
        TopoDS_Shape shape = myIgesReader.Shape(i);
        TopoDS_Face& face = static_cast<TopoDS_Face&>(TopoDS::Face(shape));
        Test *test;
        // tried each of these also and did not succeed
        // Test *test = dynamic_cast<Test*>(&face);
        // test = &face

        test->TopoDS_Face::operator=(face);
        ...
    }

}

main 中的代码从 IGES 文件中提取人脸,并将每个人脸对象分配给 main 中创建的测试对象。编译进行得很好,但 valgrind 抱怨以下错误:

==21718== Use of uninitialised value of size 8
==21718==    at 0x9B0946C: Handle_Standard_Transient::Assign(Standard_Transient const*) (in /usr/lib64/libTKernel.so.10.0.0)
==21718==    by 0x408111: Handle_TopoDS_TShape::operator=(Handle_TopoDS_TShape const&) (in /home/ub/Projects/C++/test/test)
==21718==    by 0x409478: TopoDS_Shape::operator=(TopoDS_Shape const&) (in /home/ub/Projects/C++/test/test)
==21718==    by 0x4094CA: TopoDS_Face::operator=(TopoDS_Face const&) (in /home/ub/Projects/C++/test/test)
==21718==    by 0x407273: main (in /home/ub/Projects/C++/test/test)
==21718== 
==21718== Use of uninitialised value of size 8
==21718==    at 0x9B09431: Handle_Standard_Transient::EndScope() (in /usr/lib64/libTKernel.so.10.0.0)
==21718==    by 0x9B09475: Handle_Standard_Transient::Assign(Standard_Transient const*) (in /usr/lib64/libTKernel.so.10.0.0)
==21718==    by 0x408111: Handle_TopoDS_TShape::operator=(Handle_TopoDS_TShape const&) (in /home/ub/Projects/C++/test/test)
==21718==    by 0x409478: TopoDS_Shape::operator=(TopoDS_Shape const&) (in /home/ub/Projects/C++/test/test)
==21718==    by 0x4094CA: TopoDS_Face::operator=(TopoDS_Face const&) (in /home/ub/Projects/C++/test/test)
==21718==    by 0x407273: main (in /home/ub/Projects/C++/test/test)
==21718== 
==21718== Invalid read of size 4
==21718==    at 0x9B0943C: Handle_Standard_Transient::EndScope() (in /usr/lib64/libTKernel.so.10.0.0)
==21718==    by 0x9B09475: Handle_Standard_Transient::Assign(Standard_Transient const*) (in /usr/lib64/libTKernel.so.10.0.0)
==21718==    by 0x408111: Handle_TopoDS_TShape::operator=(Handle_TopoDS_TShape const&) (in /home/ub/Projects/C++/test/test)
==21718==    by 0x409478: TopoDS_Shape::operator=(TopoDS_Shape const&) (in /home/ub/Projects/C++/test/test)
==21718==    by 0x4094CA: TopoDS_Face::operator=(TopoDS_Face const&) (in /home/ub/Projects/C++/test/test)
==21718==    by 0x407273: main (in /home/ub/Projects/C++/test/test)
==21718==  Address 0x75eb394801c38350 is not stack'd, malloc'd or (recently) free'd
==21718== 
==21718== 
==21718== Process terminating with default action of signal 11 (SIGSEGV)
==21718==  General Protection Fault
==21718==    at 0x9B0943C: Handle_Standard_Transient::EndScope() (in /usr/lib64/libTKernel.so.10.0.0)
==21718==    by 0x9B09475: Handle_Standard_Transient::Assign(Standard_Transient const*) (in /usr/lib64/libTKernel.so.10.0.0)
==21718==    by 0x408111: Handle_TopoDS_TShape::operator=(Handle_TopoDS_TShape const&) (in /home/ub/Projects/C++/test/test)
==21718==    by 0x409478: TopoDS_Shape::operator=(TopoDS_Shape const&) (in /home/ub/Projects/C++/test/test)
==21718==    by 0x4094CA: TopoDS_Face::operator=(TopoDS_Face const&) (in /home/ub/Projects/C++/test/test)
==21718==    by 0x407273: main (in /home/ub/Projects/C++/test/test)
==21718== 

我需要帮助解决程序。提前致谢。

4

1 回答 1

1

如果您查看TopoDS_Shape层次结构定义,您会注意到:

  • TopoDS_Shape不是接口(没有虚拟方法或虚拟析构函数)。

  • TopoDS_Face、TopoDS_Shell 和其他提供从 TopoDS_Shape 的“虚拟”继承,而不添加任何字段或方法。

  • OCCT 不存储强类型 TopoDS_ 形状对象。OCCT 集合(首先包括 TopoDS_Shape 的子形状!)按值存储 TopoDS_Shape,而不是引用。像TopoDS::Face()这样的方法基本上会根据 TopoDS_Shape::ShapeType() 方法返回的枚举进行内存别名以返回具有运行时检查的强类型对象。

  • 实际上,可共享引用以 TopoDS_Shape 类的 Handle(TopoDS_TShape) 属性的形式存储。TopoDS_TShape是定义主要形状数据(几何)的真实类,它确实是具有真实子类的虚拟类。

  • TopoDS_TShape层次结构(具有诸如 TopoDS_TShape、TopoDS_TShell 等子类)是内部的和最终的 - 没有用于在应用程序级别扩展此层次结构的接口。我想说,在 C++11 中,这些类应该被标记为 final。

基于此描述,应该清楚的是,更改 TopoDS_Shape / TopoDS_TShape 层次结构与 OCCT 设计(对于这些特定类)相矛盾,并且不能在应用程序级别可靠地完成。假设的替代方法是使用地图(如NCollection_DataMap<TopoDS_Shape,TopTools_ShapeMapHasher,MyShapeProperties>)或文档结构(如XCAF文档使用TDataStd_NamedData和其他属性定义装配结构)存储与形状相关的附加信息。

于 2020-02-13T20:32:32.490 回答