关于C++的"perator"的问题????

hua_zhixing_ 2009-01-12 07:32:36
问题在“???”处。随便帮我详细解析一下operator.
//Demonstrate member operator overloading
#include<iostream>
#include<cstdlib>
#include<cstring>
using namespace std;

class strop
{
private:
char value[12];
public:
strop() { value[0]=0; }
strop(const char *s);

long operator+(strop b);
long operator-(strop b);
};

int main(void)
{
strop a="1234";
strop b="4321";
cout<<"\na+b=="<<a+b+6;
cout<<"\na-b=="<<a-b+10;
return 0;
}

strop::strop(const char *s)
{
strncpy_s(value,s,11);
value[11]=0;
}

long strop::operator+(strop b)
{ return(atol(value)+atol(b.value)); }//????这里的atol(value)是怎么回事呀?

long strop::operator-(strop b)
{ return(atol(value)-atol(b.value)); }
...全文
204 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
shyli 2009-01-12
  • 打赏
  • 举报
回复
atol,查文档。
没文档就百度呀
xiaoyisnail 2009-01-12
  • 打赏
  • 举报
回复
operator是运算符重载,具体参见c++ primer第四版 14章
xiaoyisnail 2009-01-12
  • 打赏
  • 举报
回复
函数名: atoi
功 能: 把字符串转换成长整型数
用 法: int atoi(const char *nptr);
程序例:

#include <stdlib.h>
#include <stdio.h>

int main(void)
{
int n;
char *str = "12345.67";

n = atoi(str);
printf("string = %s integer = %d\n", str, n);
return 0;
}



waizqfor 2009-01-12
  • 打赏
  • 举报
回复
C++中的operator 有两种用法 一种是operator overloading(操作符重载) 一种是operator casting(操作隐式转换).

template<typename T> class A
{
public:
const T operator + (const T& rhs)//重载+号
{
return this->m_ + rhs;
}
private:
T m_;
};


class A
{
public:
operator B* () { return this->b_;}
operator const B* () {return this->b_;}
operator B& () {return *this->b_;}
private:
B* b_;
};

A a;
//当if(a),编译时,其中它转换成if(a.operator B*()),其实也就是判断 if(a.b_)
imafish_i 2009-01-12
  • 打赏
  • 举报
回复
是个函数,把一个c字符串转换成一个长整型。
in msdn 2005:

Convert a string to a long integer.

long atol(
const char *str
);

另外代码重载了+和-两个操作符
elegant87 2009-01-12
  • 打赏
  • 举报
回复
atol()函数的功能是将字符串转换为long型的数据!

MSDN上面很全面的:

atof, atoi, _atoi64, atol
Convert strings to double (atof), integer (atoi, _atoi64), or long (atol).

double atof( const char *string );

int atoi( const char *string );

__int64 _atoi64( const char *string );

long atol( const char *string );

nullah 2009-01-12
  • 打赏
  • 举报
回复
函数名: atol
  功 能: 把字符串转换成长整型数
  用 法: long atol(const char *nptr);

65,210

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧