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闭包特性源于内部函数可以将外部函数的活动对象保存在自己的作用域链上,所以使内部函数的可以将外部函数的活动对象占为己有,可以在外部函数销毁时依然存有外部函数内的活动对象内容,这样做的好处是可 ...
随机推荐
- Swift - 初始化方法返回nil表示初始化失败
自Swift1.1开始,初始化init方法便有返回nil的能力.我们可以在init声明的时候在其后面加上一个 ? 或者 ! 来表示初始化失败时可能返回nil. 比如,给Int类添加一个将字符串初始化成 ...
- swift 有些语法还是不支持。
<pre name="code" class="html">"func hasAnyMatches(list: Int[], condit ...
- 重操JS旧业第四弹:Date与Global对象
1 Date原理 Date类型表示时间,js中采用UTC国际协调时间,以1971年1月1日0分0秒0微秒开始,经过的毫秒数来表示时间,比如一年的时间计算 1分:1000*60: 1小时:1000(毫秒 ...
- javaEE开发之导出excel工具类
web开发中,一个系统的普通需求也包含导出excel,一般採用POI做统计报表导出excel. 导出excel工具类: import java.io.FileOutputStream; import ...
- 王立平--PopupWindow
MainActivity.java <span style="font-size:14px;">package com.main; import android.app ...
- May Day Holiday
Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Submit Status Practic ...
- shell的特殊符号的表示
shell中存在一些特殊的符号.这些符号可以帮助我们更好的写出shell来 1.特殊字符 符号 使用 输出 , 枚举分隔符 . 当前目 ...
- 14.9.4 COMPACT and REDUNDANT Row Formats
14.9.4 COMPACT and REDUNDANT Row Formats InnoDB 早期的版本 使用一种未命名的文件格式(现在称为Antelope(羚羊)) 对于数据库文件 在这种文件格式 ...
- JavaScript快速入门(二)——JavaScript变量
变量声明 JavaScript的变量声明分为显式声明跟隐式声明. 显式声明 即带var关键字声明,例如 var example = example; 要注意JavaScript里面声明的关键字只有fu ...
- WordPress数据备份
服务器钱用光了要关了或者是服务器想要搬家,需要备份各种数据. 今天简单的备份了一下在服务器上面wordpress各种文件和资源. wordpress的数据主要分两个部分,一个是文字部分的:一个是附件部 ...