• <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 閱讀(2437) 評(píng)論(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++允許我們重新定義操作符用于類類型對(duì)象時(shí)的含義。如果需要,可以像內(nèi)置轉(zhuǎn)換那樣使用類類型轉(zhuǎn)換,將一個(gè)類型對(duì)象隱式轉(zhuǎn)換到另一類型。如在OpenCascade中經(jīng)常看到如下類似的代碼:

            TopoDS_Shape theSphere = BRepPrimAPI_MakeSphere(1.0);

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

            使用類型轉(zhuǎn)換操作符在將一種類型轉(zhuǎn)換到另一種類型時(shí),感覺自然。當(dāng)類較多且經(jīng)常需要進(jìn)行類型之間的轉(zhuǎn)換時(shí),定義類型轉(zhuǎn)換操作符還是很方便的。本文結(jié)合OpenCascade程序來體驗(yàn)使用類型轉(zhuǎn)換操作符帶來的便利。


            2. Conversion Operators

            轉(zhuǎn)換操作符(Conversion Operators)提供了從一種對(duì)象類型自動(dòng)轉(zhuǎn)換到另一種類型的方式。一個(gè)經(jīng)典例子就是自定義字符串類,但是可以將這個(gè)自定義的字符串類當(dāng)作函數(shù)參數(shù)傳給const char*類型的函數(shù),如標(biāo)準(zhǔn)C中的一些函數(shù):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);

            轉(zhuǎn)換操作符是一種特殊的類成員函數(shù)。它定義將類類型值轉(zhuǎn)換為其他類型值的轉(zhuǎn)換。轉(zhuǎn)換操作符在類定義體內(nèi)聲明,在關(guān)鍵字operator之后跟著轉(zhuǎn)換的目標(biāo)類型。轉(zhuǎn)換操作符的通用形式為:

            operator type(); 

            轉(zhuǎn)換函數(shù)必須是成員函數(shù),不能指定返回類型,且形參表必須為空。因?yàn)檗D(zhuǎn)換的目標(biāo)類型已經(jīng)出現(xiàn)在轉(zhuǎn)換操作符中了,所以就不需要重復(fù)定義返回值類型了。


            3. Conversion Operators in OpenCascade

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

             

            /*
            *    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自動(dòng)轉(zhuǎn)換成TopoDS_Shape, TopoDS_Solid, TopoDS_Shell, TopoDS_Face,但是不能自動(dòng)轉(zhuǎn)換成TopoDS_Wire。這是因?yàn)樵谄涓割怋RepPrimAPI_MakeOneAxis中定義這些轉(zhuǎn)換操作符,代碼如下所示:

             

            //! 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:
            };

            由上述代碼可知,當(dāng)將BRepPrimAPI_MakeSphere賦值給TopoDS_Shape時(shí),會(huì)調(diào)用operator TopoDS_Shape()轉(zhuǎn)換操作符的轉(zhuǎn)換函數(shù);當(dāng)賦值給TopoDS_Shell時(shí),會(huì)調(diào)用operator TopoDS_Shell()轉(zhuǎn)換函數(shù),等等。未定義的轉(zhuǎn)換類型是不允許自動(dòng)轉(zhuǎn)換的,如TopoDS_Wire。

            使用這些轉(zhuǎn)換操作符使不同類型之間的類型轉(zhuǎn)換很自然直觀,看上去就像調(diào)用了一個(gè)函數(shù)。

            類型之間的轉(zhuǎn)換當(dāng)然還有其他方法,如給轉(zhuǎn)換的目標(biāo)類型增加一個(gè)構(gòu)造函數(shù)來實(shí)現(xiàn)。但是使用構(gòu)造函數(shù)來轉(zhuǎn)換不能轉(zhuǎn)換成基本類型,如int, double等;還有個(gè)不足之處就是要修改轉(zhuǎn)換目標(biāo)類的聲明文件來增加一個(gè)構(gòu)造函數(shù)。沒有轉(zhuǎn)換操作符來得自然,方便。


            4. Conclusion

            當(dāng)需要在不同類型之間進(jìn)行類型轉(zhuǎn)換時(shí),可以使用轉(zhuǎn)換操作符(Conversion Operators)。使用轉(zhuǎn)換操作符的方式別其他方法要簡(jiǎn)單直觀。

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


            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

            久久久久亚洲AV成人网人人网站 | 久久综合九色综合久99| 久久精品午夜一区二区福利| 久久偷看各类wc女厕嘘嘘| 久久香蕉国产线看观看精品yw| 久久婷婷成人综合色综合| 久久综合狠狠综合久久综合88| 成人妇女免费播放久久久| 久久91精品国产91久久户| 色婷婷综合久久久久中文字幕| 久久综合九色欧美综合狠狠| 久久超碰97人人做人人爱| 亚洲午夜精品久久久久久人妖| 久久精品国产久精国产果冻传媒 | 欧美熟妇另类久久久久久不卡| 日本三级久久网| 久久精品国产亚洲av日韩| 欧美日韩精品久久久久| 日韩亚洲欧美久久久www综合网| 精品国产青草久久久久福利| 99久久99久久精品国产| 亚洲AV无码久久精品狠狠爱浪潮| 精品欧美一区二区三区久久久 | 99久久综合狠狠综合久久止| 国内精品久久国产| 欧美一级久久久久久久大片 | 久久久无码精品午夜| 91久久精品国产91性色也| 久久香蕉国产线看观看乱码| 亚洲国产精品成人久久| 99精品久久精品一区二区| 久久午夜无码鲁丝片秋霞| 香蕉99久久国产综合精品宅男自 | 久久久久亚洲av无码专区| 亚洲午夜久久久影院| 久久99国产精品一区二区| 丰满少妇人妻久久久久久4| 久久九九免费高清视频| 亚洲伊人久久成综合人影院 | 中文字幕日本人妻久久久免费| 亚洲一本综合久久|