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