一道关于string的面试问题

czk037 2006-11-16 12:33:03
string strA; strA="sgfrsgf";
string strB="sgfrsgf";
string strC("sgfrsgf");

strA,strB,strC有什么差异?
...全文
2646 37 打赏 收藏 转发到动态 举报
写回复
用AI写文章
37 条回复
切换为时间正序
请发表友善的回复…
发表回复
lxhtzy 2006-11-25
  • 打赏
  • 举报
回复
楼上明解了
linghaowangluo 2006-11-25
  • 打赏
  • 举报
回复
学习了
sunbird69 2006-11-25
  • 打赏
  • 举报
回复
收下
liuyingwu 2006-11-23
  • 打赏
  • 举报
回复
强 受益了 谢谢
chary8088 2006-11-22
  • 打赏
  • 举报
回复
第一个:赋值 ,operation = 被调用。
第二个:调用拷贝构造函数。
第三个:调用构造函数。

HappyTree(天行健,君子以自强不息)
dushman 2006-11-22
  • 打赏
  • 举报
回复
天行健,强!
收益了
飞哥 2006-11-21
  • 打赏
  • 举报
回复
weiym(为什么美女都喜欢和我在一起呢,好奇怪)
人 见 人 爱
meric 2006-11-21
  • 打赏
  • 举报
回复
受教,脚印脚印
qwertxp 2006-11-21
  • 打赏
  • 举报
回复
凡路过必留下痕迹。。。
受教~~~
Godlikeme 2006-11-21
  • 打赏
  • 举报
回复
mark
做鸡真好吃 2006-11-17
  • 打赏
  • 举报
回复
mark~
HappyTree 2006-11-17
  • 打赏
  • 举报
回复
to goldcool():
第二个认为是direct constructor只有在编译器对此优化的情况下才是对的。按照C++标准,这里应该是先调用direct constructor生成临时对象,然后由临时对象调用copy constructor生成最终的对象。
我贴的测试程序容易引起误导,因为vc做了优化。如果你换一个编译器,可能得到的是direct constructor + copy constructor。
它事实上相当于Int t4(Int(u));
sosdairs 2006-11-17
  • 打赏
  • 举报
回复
学习中~

三个变量中内容是一样的

看了HappyTree(天行健,君子以自强不息)的测试程序
才明白了它们不同之处
凌风_ 2006-11-17
  • 打赏
  • 举报
回复
学习~~
感觉
--------------------
第一个:赋值 ,operation = 被调用。
第二个:调用拷贝构造函数。
第三个:调用构造函数。
--------------------------

-----------------
呃,去看C++ Primer 3e p569
结果相同,行为有别。
默认构造+赋值
(隐式)构造
(显式)构造
-------------------


说的一目了然。。。
tianshanfe 2006-11-17
  • 打赏
  • 举报
回复
Type variable(value);
那int a(5);也是可以的咯
OOPhaisky 2006-11-17
  • 打赏
  • 举报
回复
string strA; strA="sgfrsgf";
string strB="sgfrsgf";
string strC("sgfrsgf");

strA,strB,strC有什么差异?
--------------------------------------------------------------------------------
strA,strB,strC的结果都是相同的,但是构造的过程不相同。
strA : 默认构造函数 + 赋值;
strB : "sgfrsgf"首先被转换为一个临时的string对象(利用string的构造函数:string(const char *) ),然后再调用copy constructor,用这个临时的string对象去初始化strB。(注:大部分的编译器会将第一个临时的string对象优化掉)
strC : 调用string的构造函数:string(const char*) 对strC进行初始化。
goldcool 2006-11-17
  • 打赏
  • 举报
回复
楼上的(HappyTree(天行健,君子以自强不息))厉害,最终解释

第一个:的确是调用 default constructor + copy assign operator

第二个:并没有调用copy constructor ,而是直接用 "显示声明的"(不要和explicit混淆) constructor

第三个:同第二个一样。

来来去去还是那句话,C++的初始化支持两种形式。
Type variable=value;
Type variable(value);

可以结帖了
HappyTree 2006-11-17
  • 打赏
  • 举报
回复
来个测试程序,楼主跑跑看

#include <iostream>
using namespace std;

class Int
{
public:
Int()
{
cout << "Default Constructor" << endl;
}
Int(int a) : a_(a)
{
cout << "Direct Constructor" << endl;
}
Int(const Int& other)
{
cout << "Copy Constructor" << endl;
a_ = other.a_;
}
Int& operator =(const Int& other)
{
cout << "Copy Assign Operator" << endl;
a_ = other.a_;
return *this;
}

private:
int a_;
};

int main(int argc, char* argv[])
{
int u = 10;

cout << "Int t1: ";
Int t1;
cout << "Int t2(): Prototyped Function" << endl;
Int t2();
cout << "Int t3(u): ";
Int t3(u);
cout << "Int t4 = u: ";
Int t4 = u;
cout << "Int t5(t4): ";
Int t5(t4);
cout << "Int t6 = t4: ";
Int t6 = t4;
cout << "t1 = t4: ";
t1 = t4;

return getchar();
}
HappyTree 2006-11-17
  • 打赏
  • 举报
回复
同意一楼的
taodm 2006-11-17
  • 打赏
  • 举报
回复
最后结果相同。
效率,只能说第三个绝不比其它的慢,第一个和第二个得看编译器及优化项。
一般3 = 2 > 1
加载更多回复(17)

64,654

社区成员

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

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