string 相加

combobox2013 2013-01-24 04:18:51
string str;
str="cc"+string("hello");

这里做了什么??

是否是:构造了一个string("cc"),然后和strign("hello")相加,然后再赋值

如果是的话,那么为什么会自动构造了一个string("cc")啊

...全文
187 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
mymtom 2013-01-24
  • 打赏
  • 举报
回复
从上面的实现看 str="cc"+string("hello"); 没有必要调用string("cc")构造函数的。
mymtom 2013-01-24
  • 打赏
  • 举报
回复
双目操作符,自然不是成员函数啊!
mymtom 2013-01-24
  • 打赏
  • 举报
回复

#include <iostream>
using namespace std;

class Test
{
    friend Test operator+(int val, Test const& another);
    friend Test operator+(Test const& another,int val);
    friend ostream& operator<<(ostream& os, const Test& obj);
private:
    int m_val;
 
public:
    Test()
    {
        m_val=10;
    }
 
public:
};

Test operator+(int val, Test const& another)
{
    Test obj;
    obj.m_val=val+another.m_val;
    return obj;

}

Test operator+(Test const& another,int val)
{
    Test obj;
    obj.m_val=val+another.m_val;
    return obj;
}

ostream& operator<<(ostream& os, const Test& obj)
{
    cout << obj.m_val;
    return os;
}
 
int main()
{
    Test obj;
    Test obj2;
    obj2=2+obj;

    cout << obj2 << endl;
    return 0;
}
combobox2013 2013-01-24
  • 打赏
  • 举报
回复
引用 4 楼 mymtom 的回复:
这楼主不需要关心, string 双目'+'操作符支持这种格式。 C/C++ code?1234567891011template< class CharT, class Traits, class Alloc > basic_string<CharT,Traits,Alloc> operator+( const CharT* lhs, ……
问题2: 是否运算符重载最多只能够有2个参数?????
combobox2013 2013-01-24
  • 打赏
  • 举报
回复
引用 5 楼 mymtom 的回复:
这个还真不一定需要调用string("cc")构造函数,从实现效率上来说,如果调用string("cc")构造函数,性能上会损失不少!
class Test
{
private:
	int m_val;

public:
	Test()
	{
		m_val=10;
	}

public:
	Test& operator+(int val, Test const& another)
	{
		m_val=val+another.m_val;
		return *this;

	}

	Test& operator+(Test const& another,int val)
	{
		m_val=val+another.m_val;
		return *this;

	}

};

int main()
{
	Test obj;
	Test obj2;
	obj2=2+obj;
	return 0;
}
提示错误 error C2804: 二进制“operator +”的参数太多
兆帅 2013-01-24
  • 打赏
  • 举报
回复
引用 4 楼 mymtom 的回复:
这楼主不需要关心, string 双目'+'操作符支持这种格式。 C/C++ code?1234567891011template< class CharT, class Traits, class Alloc > basic_string<CharT,Traits,Alloc> operator+( const CharT* lhs, ……
mymtom 2013-01-24
  • 打赏
  • 举报
回复
这个还真不一定需要调用string("cc")构造函数,从实现效率上来说,如果调用string("cc")构造函数,性能上会损失不少!
mymtom 2013-01-24
  • 打赏
  • 举报
回复
这楼主不需要关心, string 双目'+'操作符支持这种格式。
template< class CharT, class Traits, class Alloc >

    basic_string<CharT,Traits,Alloc>
        operator+( const CharT* lhs,
                   const basic_string<CharT,Traits,Alloc>& rhs );

template< class CharT, class Traits, class Alloc >

    basic_string<CharT,Traits,Alloc>
        operator+( const basic_string<CharT,Traits,Alloc>& lhs,
                   const CharT* rhs );
ppsharp 2013-01-24
  • 打赏
  • 举报
回复
我只知道字符串对象可以和字符串字面值直接相加。
赵4老师 2013-01-24
  • 打赏
  • 举报
回复
VC调试时按Alt+8、Alt+6和Alt+5,打开汇编窗口、内存窗口和寄存器窗口看每句C对应的汇编、单步执行并观察相应内存和寄存器变化,这样过一遍不就啥都明白了吗。 对VC来说,所谓‘调试时’就是编译连接通过以后,按F10或F11键单步执行一步以后的时候,或者在某行按F9设了断点后按F5执行停在该断点处的时候。 (Turbo C或Borland C用Turbo Debugger调试,Linux或Unix下用GDB调试时,看每句C对应的汇编并单步执行观察相应内存和寄存器变化。)
Joseph_ 2013-01-24
  • 打赏
  • 举报
回复
string(“Hello”); 就是string的构造函数?

65,187

社区成员

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

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