64,637
社区成员
发帖
与我相关
我的任务
分享
class MM {
public:
MM(char *aa) {cout <<"aa:" <<aa <<endl;}
MM(const MM &aa) {cout <<"bb" <<endl;}
MM & operator=(const MM &aa){ cout <<"ccc:" <<endl;}
};
int _tmain(int argc, _TCHAR* argv[])
{
MM m = MM("123");
system("pause");
return 0;
}
#include <iostream>
using namespace std;
class Test {
public:
explicit Test(char *str) {
cout <<"aa:" << str <<endl;
}
Test(const Test &str) {
cout <<"bb:" << endl;
}
};
int main(int argc, char *argv[]) {
// Test aa = "123";
// Test aa("123");
return 0;
}
Test aa = "123";
Test aa("123");
请教一下啊,这两种的区别在哪?调用的构造方法不一样么?
MM m="123";
MM m("123");
$ cat main.cpp; g++ -fno-elide-constructors -o demo.exe main.cpp; ./demo.exe
#include <iostream>
using namespace std;
class MM {
public:
MM(char const *aa) {cout <<"aa:" <<aa <<endl;}
MM(const MM &aa) {cout <<"bb" <<endl;}
MM & operator=(const MM &aa){ cout <<"ccc:" <<endl; return *this;}
};
int main(int argc, char* argv[])
{
MM m = MM("123");
return 0;
}
aa:123
bb
#include <iostream>
using namespace std;
class Test {
public:
explicit Test(char *str) {
cout <<"aa:" << str <<endl;
}
Test(const Test &str) {
cout <<"bb:" << endl;
}
};
int main(int argc, char *argv[]) {
// Test aa = "123";
// Test aa("123");
return 0;
}
Test aa = "123";
Test aa("123");
请教一下啊,这两种的区别在哪?调用的构造方法不一样么? [/quote]
一样。