Conversion Operators in OpenCascade
Conversion Operators in OpenCascade
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中经常看到如下类似的代码:
其中,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之后跟着转换的目标类型。转换操作符的通用形式为:
转换函数必须是成员函数,不能指定返回类型,且形参表必须为空。因为转换的目标类型已经出现在转换操作符中了,所以就不需要重复定义返回值类型了。
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 ;
}
如上代码所示,可以将类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() = ;
//! 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
PDF Version: Conversion Operators in OpenCascade
Conversion Operators in OpenCascade的更多相关文章
- Advanced C++ | Conversion Operators
In C++, the programmer abstracts real world objects using classes as concrete types. Sometimes it is ...
- LINQ 学习路程 -- 查询操作 Conversion Operators
Method Description AsEnumerable Returns the input sequence as IEnumerable<t> AsQueryable Conve ...
- [c++] Operator overloading
c++的操蛋属性:自己为一档,空一档,其他随意. UB_stack a; UB_stack b = a; // copy auto c = a; auto d {a}; // (or auto d = ...
- Google C++ Style Guide
Background C++ is one of the main development languages used by many of Google's open-source project ...
- CLR via C# 3rd - 08 - Methods
Kinds of methods Constructors Type constructors Overload operators Type con ...
- CLR via C# 3rd - 06 - Type and Member Basics
1. Different Kinds of Type Members A type can define zero or more of the following kinds of ...
- C++的隐式类型转换与转换操作符
C++标准允许隐式类型转换,即对特定的类,在特定条件下,某些参数或变量将隐形转换成类对象(创建临时对象).如果这种转换代价很大(调用类的构造函数),隐式转换将影响性能.隐式转换的发生条件:函数调用中, ...
- C#复习④
C#复习④ 2016年6月16日 12:37 Main Classes and Structs 类和结构体 1.Contents of Classes 字段,常量,方法,构造函数,析构函数: 特性,事 ...
- (转)C++0x语言新特性一览
转自:http://blog.csdn.net/zwvista/article/details/2429781 原文请见http://en.wikipedia.org/wiki/C%2B%2B0x. ...
随机推荐
- 0_MVC+EF+Autofac(dbfirst)轻型项目框架_基本框架
前言 原来一直使用他人的开源项目框架,异常的定位会很麻烦,甚至不知道这个异常来自我的代码还是这个框架本身.他人的框架有一定的制约性,也有可能是我对那些框架并没深入了解,因为这些开源框架在网上也很难找到 ...
- 弱省互测#0 t1
题意 给一个\(N \times M\)的01网格,1不能走,从起点\((1, 1)\)走到\((N, M)\),每次只能向下或向右走一格,问两条不相交的路径的方案数.(n, m<=1000) ...
- dvb标准
一.概念 DVB, 数字视频广播Digital Video Broadcasting的缩写, 是由DVB项目维护的一系列国际承认的数字电视公开标准.(欧标)二.分类DVB系统传输方式有如下几种: 卫星 ...
- Font Squirrel
Font Squirrel FontSquirrel:国外免费字体下载网是一个提供高质量商业字体下载网站,提供海量的英文字体库,用户可以随意下载并应用于各种商业用字,无需考虑其版权问题. 官网地址:h ...
- Qgis连接Oracle
CMake编译中选择编译Oracle一项以后,编译的qgis才会有连接Oracle数据库的功能. 编译qgis以后,可以通过添加矢量图层中选择Oracle数据库,或是添加Oracle空间图层,或是添加 ...
- 前端页面开发,最低兼容IE 8的多设备跨平台问题解决!
项目要求: 网站能够使用PC.ipad.mobile phone正常访问 页面PSD版式宽度分别为1024px和750px 参考资料 使用CSS3 Media Queries,其作用就是允许添加表达式 ...
- [转]Android ListView 与 RecyclerView 对比浅析—缓存机制
从源码角度剖析ListView 与 RecyclerView 缓存机制的不同 https://zhuanlan.zhihu.com/p/23339185 原文地址:http://dev.qq.com/ ...
- Python之路Day18-正则及Django
一. 正则表达式 1.定义正则表达式 /.../ 用于定义正则表达式 /.../g 表示全局匹配 /.../i 表示不区分大小写 /.../m 表示多行匹配JS正则匹配时本身就是支持多行,此处多行匹 ...
- 网站中使用中文个性字库字体--@font-face解决方案探索 l(转)
最近的项目有用到特别中文字体,最终效果如下图: 红线标记处均为字体,可选中,交互起来,比图片方便太多了. 解决思路就是将体积巨大的中文字库,取子集,只包涵要使用的那部分文字,因此体积就很小了(包含10 ...
- Hibernate多对多双向关联的配置
Hibernate的双向多对多关联有两种配置方法:那我们就来看看两种方案是如何配置的. 一.创建以各自类为类型的集合来关联 1.首先我们要在两个实体类(雇员<Emploee>.工程< ...