C++操作符operator的另一种用法
http://blog.csdn.net/memewry/article/details/7833314 参考地址
今天在程序员面试宝典上看到这样一道题目:
A C++ developer wants to handle a static_cast<char*>() operation for the class String shown below. Which of the following options are valid declarations that will accomplish this task?
class String
{
public:
//...
//declaration goes here
};
A. char* operator();
B. char* operator char*();
C. String operator char*();
D. operator char*();
E. char* operator String();
答案是D,但是百思不得其解,百度了很多资料,才发现原来operator的作用不仅仅在于运算符的重载,他还有另外一种作用:强制类型转换。
operator char*()是类型转换函数的定义,即该类型可以自动转换为char*类型。有时候经常和const在一起用,operator const char*() const.
下面看别人写的一个例子:
- /*************************Test_OperatorConvert.h*************************/
- #ifndef TEST_OPERATORCONVERT_H
- #define TEST_OPERATORCONVERT_H
- const int MAX_PATH2 = 256;
- class Test_OperatorConvert{
- public:
- Test_OperatorConvert();
- Test_OperatorConvert(char *str);
- virtual ~Test_OperatorConvert();
- char *GetStr();
- operator char*();
- private:
- char m_szTest[MAX_PATH2];
- };
- #endif
- /*************************Test_OperatorConvert.cpp*************************/
- #include "stdafx.h"
- #include "Test_OperatorConvert.h"
- #include <iostream>
- using namespace std;
- Test_OperatorConvert::Test_OperatorConvert()
- {
- memset(m_szTest, 0, sizeof(m_szTest));
- }
- Test_OperatorConvert::Test_OperatorConvert(char *str)
- {
- strcpy(m_szTest, str);
- }
- Test_OperatorConvert::~Test_OperatorConvert()
- {
- }
- // 这个函数实现的功能与operator char*()的功能一致。
- char *Test_OperatorConvert::GetStr()
- {
- return m_szTest;
- }
- Test_OperatorConvert::operator char*()
- {
- return m_szTest;
- }
- int main(int argc, char* argv[])
- {
- Test_OperatorConvert cTestInstance;
- char *pTest1 = cTestInstance; // 这里就是operator char*()发挥作用的地方,
- // 类Test_OperatorConvert 被转换成char*类型。
- char *pTest2 = cTestInstance.GetStr(); //如果没有实现operator char*(),使用这种方法也一样。
- return 0;
- }

/*************************Test_OperatorConvert.h*************************/
#ifndef TEST_OPERATORCONVERT_H
#define TEST_OPERATORCONVERT_H const int MAX_PATH2 = 256; class Test_OperatorConvert{
public:
Test_OperatorConvert();
Test_OperatorConvert(char *str);
virtual ~Test_OperatorConvert();
char *GetStr();
operator char*();
private:
char m_szTest[MAX_PATH2];
};
#endif /*************************Test_OperatorConvert.cpp*************************/
#include "stdafx.h"
#include "Test_OperatorConvert.h" #include <iostream>
using namespace std; Test_OperatorConvert::Test_OperatorConvert()
{
memset(m_szTest, 0, sizeof(m_szTest));
} Test_OperatorConvert::Test_OperatorConvert(char *str)
{
strcpy(m_szTest, str);
} Test_OperatorConvert::~Test_OperatorConvert()
{
} // 这个函数实现的功能与operator char*()的功能一致。
char *Test_OperatorConvert::GetStr()
{
return m_szTest;
} Test_OperatorConvert::operator char*()
{
return m_szTest;
} int main(int argc, char* argv[])
{
Test_OperatorConvert cTestInstance;
char *pTest1 = cTestInstance; // 这里就是operator char*()发挥作用的地方,
// 类Test_OperatorConvert 被转换成char*类型。
char *pTest2 = cTestInstance.GetStr(); //如果没有实现operator char*(),使用这种方法也一样。
return 0;
}
这类似于一种隐式类型转换,实现的语法格式就是 operator type_name().
在需要char*类型的时候,就可以用Test_OperatorConvert来代替。还有一点需要注意的就是:C++中有3中函数不需要返回类型:构造函数、析构函数、类型转换函数
前两个我们都知道不允许返回任何类型,甚至void类型,也不允许出现return,最后一个也不写返回类型,但是必须返回对应类型的值,即必须有return语句。
C++操作符operator的另一种用法的更多相关文章
- [VC]C++ operator 两种用法
C++中的operator,有两种用法,一种是operator overloading(操作符重载),一种是operator casting(操作隐式转换).下面分别进行介绍: 1.operato ...
- c++ operator操作符的两种用法:重载和隐式类型转换,string转其他基本数据类型的简洁实现string_cast
C++中的operator主要有两个作用,一是操作符的重载,一是自定义对象类型的隐式转换.对于操作符的重载,许多人都不陌生,但是估计不少人都不太熟悉operator的第二种用法,即自定义对象类型的隐式 ...
- operator 的两种用法
C++,有时它的确是个耐玩的东东,就比如operator,它有两种用法,一种是operator overloading(操作符重载),一种是operator casting(操作隐式转换).1.操作符 ...
- 重载操作符 operator overloading 学习笔记
重载操作符,只是另外一种调用函数的方法和表现方式,在某些情况它可以让代码更简单易读.注意不要过度使用重载操作符,除非它让你的类更简单,让你的代码更易读. 1语法 如下: 其中友元,关键字不是必须的,但 ...
- a标签中href=""的几种用法(转)
a标签中href=""的几种用法 标签: html / a标签 / javascript 46371 众所周知,a标签的最重要功能是实现超链接和锚点.而且,大多数人认为a标签最 ...
- using 的三种用法
using 有哪三种用法? 1)引入命名空间. 2)给命名空间或者类型起别名. 3)划定作用域.自动释放资源,使用该方法的类型必须实现了 System.IDisposable接口,当对象脱离作用域之后 ...
- Wix 安装部署教程(十五) --CustomAction的七种用法
在WIX中,CustomAction用来在安装过程中执行自定义行为.比如注册.修改文件.触发其他可执行文件等.这一节主要是介绍一下CustomAction的7种用法. 在此之前要了解InstallEx ...
- Android Intent的几种用法全面总结
Android Intent的几种用法全面总结 Intent, 用法 Intent应该算是Android中特有的东西.你可以在Intent中指定程序要执行的动作(比如:view,edit,dial), ...
- Js闭包常见三种用法
Js闭包特性源于内部函数可以将外部函数的活动对象保存在自己的作用域链上,所以使内部函数的可以将外部函数的活动对象占为己有,可以在外部函数销毁时依然存有外部函数内的活动对象内容,这样做的好处是可 ...
随机推荐
- ASP.NET - 对URL传递的值进行编码Server.UrlEncode()
/// <summary> /// 搜索内容 /// </summary> /// <param name="sender"></para ...
- net core与golang web
Asp.net core与golang web简单对比测试 最近因为工作需要接触了go语言,又恰好asp.net core发布RC2,就想简单做个对比测试. 下面是测试环境: CPU:E3-1230 ...
- element.style覆盖了我的样式!!
原文:element.style覆盖了我的样式!! 有时候在写css时,显示效果会出现非常诡异的效果 不知道有没有遇到这种 css: #logo{ border: solid 1px blue; } ...
- 网页插入QQ 无需加好友
<p>联系方式:1073351325<a href="tencent://message/?Menu=yes&uin=1073351325&Site=dsf ...
- ios23-文件上传
1.上传图片 3. // // ios23_uploadViewController.h // ios23-upload // // Created by on 13-6-17. // Co ...
- mssql数据库游标批量改动符合条件的记录
//需求:因为项目刚上传,没有票数,为了表现出一定的人气,须要在一開始把各项目的票数赋一个值 , 但每一个项目不能一样,否则easy看出问题,呵呵 . DECLARE @Id varchar(50) ...
- rsync使用指南
考虑到服务器数据的安全,我考虑增加一台备份服务器,通过数据同步,达到较好的冗余. linux下有非常好的一个命令rsync可以实现差异备份,下面就说说它的用法:ubuntu缺省安装的安装中,rsync ...
- 从WinMain开始
一.抽象渗漏法则 根据Joel的抽象渗漏法则,所有重大的抽象机制在某种程度上都是有漏洞的.Joel举过一个例子: C++字符串类型应该能让你假装字符串是个基本类型,它们尝试“字串很难处理”这个事实抽象 ...
- Linux入门基础 #9:管道及重定向
本文出自 http://blog.csdn.net/shuangde800 ------------------------------------------------------------ ...
- codeforces 598A Tricky Sum
题目链接:http://codeforces.com/contest/598/problem/A 题目分类:大数 题意:1到n 如果是2的次方则减去这个数,否则就加上这个数,求最后的结果是多少 题目分 ...