• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            eryar

            PipeCAD - Plant Piping Design Software.
            RvmTranslator - Translate AVEVA RVM to OBJ, glTF, etc.
            posts - 603, comments - 590, trackbacks - 0, articles - 0

            Conversion Operators in OpenCascade

            Posted on 2014-04-12 19:21 eryar 閱讀(2419) 評論(0)  編輯 收藏 引用 所屬分類: 2.OpenCASCADE

            Conversion Operators in OpenCascade

            eryar@163.com

            Abstract. C++ lets us redefine the meaning of the operators when applied to objects. It also lets us define conversion operations for class types. Class-type conversions are used like the built-in conversions to implicitly convert an object of one type to another type when needed. A conversion operator provides a way for you to define how an object can be converted automatically to a different type. The paper gives some conversion operators examples in OpenCascade.

            Key words. OpenCascade, Conversion Operators, Operator overloading

            1. Introduction

            C++允許我們重新定義操作符用于類類型對象時的含義。如果需要,可以像內置轉換那樣使用類類型轉換,將一個類型對象隱式轉換到另一類型。如在OpenCascade中經常看到如下類似的代碼:

            TopoDS_Shape theSphere = BRepPrimAPI_MakeSphere(1.0);

            其中,BRepPrimAPI_MakeSphere也是一個類,直接賦值給了另一個類TopoDS_Shape的對象theSphere。第一次這么來用的時候有些困惑,不知道你有沒有這樣的疑問,不管你有沒有,反正我是有的(Just kidding)。后來才知道,這就是一種重載方式,重載了類型轉換操作符(Conversion Operator)。

            使用類型轉換操作符在將一種類型轉換到另一種類型時,感覺自然。當類較多且經常需要進行類型之間的轉換時,定義類型轉換操作符還是很方便的。本文結合OpenCascade程序來體驗使用類型轉換操作符帶來的便利。


            2. Conversion Operators

            轉換操作符(Conversion Operators)提供了從一種對象類型自動轉換到另一種類型的方式。一個經典例子就是自定義字符串類,但是可以將這個自定義的字符串類當作函數參數傳給const char*類型的函數,如標準C中的一些函數:strcmp(), strlen()。示例程序如下所示:

             

            class MyString
            {
            public:
                MyString(
            const char* string);
                
                
            // convert MyString to a C-style string.
                operator const char*() { return mBuffer; }

            private:
                
            char* mBuffer;
                
            int mLength;
            };

            // MyString objects get automatically converted to const char*
            MyString mystr("Haggis");
            int same = strcmp(mystr, "Edible");
            int len = strlen(mystr);

            轉換操作符是一種特殊的類成員函數。它定義將類類型值轉換為其他類型值的轉換。轉換操作符在類定義體內聲明,在關鍵字operator之后跟著轉換的目標類型。轉換操作符的通用形式為:

            operator type(); 

            轉換函數必須是成員函數,不能指定返回類型,且形參表必須為空。因為轉換的目標類型已經出現在轉換操作符中了,所以就不需要重復定義返回值類型了。


            3. Conversion Operators in OpenCascade

            OpenCascade中很多地方用到了轉換操作符,如將生成的基本實體轉換成其他拓樸類型時就用到了轉換操作符,程序代碼如下所示:

             

            /*
            *    Copyright (c) 2014 eryar All Rights Reserved.
            *
            *           File : Main.cpp
            *         Author : eryar@163.com
            *           Date : 2014-04-12 18:02
            *        Version : V1.0
            *
            *    Description : Learn Conversion Operators in OpenCascade.
            *
            *      Key words : OpenCascade, Conversion Operators
            *
            */

            #define WNT
            #include 
            <BRepPrimAPI_MakeSphere.hxx>

            #pragma comment(lib, 
            "TKernel.lib")
            #pragma comment(lib, 
            "TKMath.lib")
            #pragma comment(lib, 
            "TKBRep.lib")
            #pragma comment(lib, 
            "TKPrim.lib")
            #pragma comment(lib, 
            "TKTopAlgo.lib")

            void TestConversionOperators(void)
            {
                TopoDS_Shape theSphereShape 
            = BRepPrimAPI_MakeSphere(1.0);
                TopoDS_Solid theSphereSolid 
            = BRepPrimAPI_MakeSphere(1.0);
                TopoDS_Shell theSphereShell 
            = BRepPrimAPI_MakeSphere(1.0);
                TopoDS_Face theSphereFace 
            = BRepPrimAPI_MakeSphere(1.0);

                
            // error C2440: 'initializing' : cannot convert 
                
            // from 'BRepPrimAPI_MakeSphere' to 'TopoDS_Wire'
                
            //TopoDS_Wire theSphereWire = BRepPrimAPI_MakeSphere(1.0);
            }

            int main(int argc, char* argv[])
            {
                TestConversionOperators();

                
            return 0;
            }

            如上代碼所示,可以將類BRepPrimAPI_MakeSphere自動轉換成TopoDS_Shape, TopoDS_Solid, TopoDS_Shell, TopoDS_Face,但是不能自動轉換成TopoDS_Wire。這是因為在其父類BRepPrimAPI_MakeOneAxis中定義這些轉換操作符,代碼如下所示:

             

            //! The abstract class MakeOneAxis is the root class of <br>
            //! algorithms used to construct rotational primitives. <br>
            class BRepPrimAPI_MakeOneAxis  : public BRepBuilderAPI_MakeShape {
            public:

              DEFINE_STANDARD_ALLOC

              
            //! The inherited commands should provide the algorithm. <br>
            //!          Returned as a pointer. <br>
              Standard_EXPORT   virtual  Standard_Address OneAxis()  = 0;
              
            //! Stores the solid in myShape. <br>
              Standard_EXPORT   virtual  void Build() ;
              
            //! Returns the lateral face of the rotational primitive. <br>
            //! <br>
              Standard_EXPORT    const TopoDS_Face& Face() ;
            Standard_EXPORT 
            operator TopoDS_Face();
              
            //! Returns the constructed rotational primitive as a shell. <br>
              Standard_EXPORT    const TopoDS_Shell& Shell() ;
            Standard_EXPORT 
            operator TopoDS_Shell();
              
            //! Returns the constructed rotational primitive as a solid. <br>
              Standard_EXPORT    const TopoDS_Solid& Solid() ;
            Standard_EXPORT 
            operator TopoDS_Solid();

            protected:

            private:
            };

            由上述代碼可知,當將BRepPrimAPI_MakeSphere賦值給TopoDS_Shape時,會調用operator TopoDS_Shape()轉換操作符的轉換函數;當賦值給TopoDS_Shell時,會調用operator TopoDS_Shell()轉換函數,等等。未定義的轉換類型是不允許自動轉換的,如TopoDS_Wire。

            使用這些轉換操作符使不同類型之間的類型轉換很自然直觀,看上去就像調用了一個函數。

            類型之間的轉換當然還有其他方法,如給轉換的目標類型增加一個構造函數來實現。但是使用構造函數來轉換不能轉換成基本類型,如int, double等;還有個不足之處就是要修改轉換目標類的聲明文件來增加一個構造函數。沒有轉換操作符來得自然,方便。


            4. Conclusion

            當需要在不同類型之間進行類型轉換時,可以使用轉換操作符(Conversion Operators)。使用轉換操作符的方式別其他方法要簡單直觀。

            由于OpenCascade中類型比較多,且經常需要要不同類型之間進行轉換操作,所以將一些常用的轉換定義成轉換操作符還是很方便的。


            5. References

            1. Bjarne Stroustrup. The C++ programming language. Higher Education Press. 2009

            2. Stanley B. Lippman, Josee Lajoie, Barbara E. Moo. C++ Primer. Addison Wesley. 2005

            3. Martin Reddy. API Design for C++. Morgan Kaufmann. 2011

            久久这里有精品| 日本一区精品久久久久影院| 欧美噜噜久久久XXX| 中文字幕久久亚洲一区| 一本大道久久东京热无码AV| 亚洲国产成人久久精品99| 亚洲午夜久久久久妓女影院 | 亚洲国产成人乱码精品女人久久久不卡 | 久久久久国产日韩精品网站| 欧洲国产伦久久久久久久| 亚洲中文久久精品无码ww16| 精品久久久久久成人AV| 久久久人妻精品无码一区| 久久综合亚洲欧美成人| 性做久久久久久免费观看| av无码久久久久不卡免费网站 | 久久久久无码精品国产不卡| 久久一区二区三区免费| 国产情侣久久久久aⅴ免费| 亚洲v国产v天堂a无码久久| 久久国产精品77777| 思思久久99热只有频精品66| 国产成人精品综合久久久| 久久ZYZ资源站无码中文动漫| 欧美伊人久久大香线蕉综合69| 国产精品久久久天天影视| 亚洲午夜久久久影院伊人| 久久综合久久综合亚洲| 亚洲国产精品无码久久九九| 精品久久久无码中文字幕| 久久综合九色综合97_久久久| 久久这里只有精品18| 亚洲中文字幕无码久久2020| 无码八A片人妻少妇久久| 伊人色综合久久天天人守人婷 | 无码人妻少妇久久中文字幕| 国产激情久久久久影院小草| 99久久综合狠狠综合久久止| 99re久久精品国产首页2020| 国产亚洲精品美女久久久| 久久精品人人做人人爽电影蜜月 |