64,282
社区成员
发帖
与我相关
我的任务
分享
cat main.cpp; g++ --no-elide-constructors main.cpp -o demo.exe; ./demo.exe
#include <iostream>
class MyInt{
public:
MyInt() {
std::cout << "MyInt()"<< " @ "<< this << std::endl;
}
MyInt(int a) {
std::cout << "MyInt(int a)"<< " @ "<< this << std::endl;
}
MyInt& operator = (MyInt const &b) {
std::cout << "MyInt& operator = (MyInt const &b)"<< " @ "<< th
is << std::endl;
return *this;
}
MyInt(MyInt const &b) {
std::cout << "MyInt(MyInt const &b)"<< " @ "<< this << std::en
dl;
}
~MyInt() {
std::cout << "~MyInt()"<< " @ "<< this << std::endl;
}
};
int main()
{
MyInt b = 10;
std::cout << "b @ " << &b << std::endl;
return 0;
}
MyInt(int a) @ 0x22ff1f
MyInt(MyInt const &b) @ 0x22ff1e
~MyInt() @ 0x22ff1f
b @ 0x22ff1e
~MyInt() @ 0x22ff1e
cat main.cpp; g++ --no-elide-constructors main.cpp -o demo.exe; ./demo.exe
#include <iostream>
class MyInt{
public:
MyInt() {
std::cout << "MyInt()"<< " @ "<< this << std::endl;
}
MyInt(int a) {
std::cout << "MyInt(int a)"<< " @ "<< this << std::endl;
}
MyInt& operator = (MyInt const &b) {
std::cout << "MyInt& operator = (MyInt const &b)"<< " @ "<<
this << std::endl;
return *this;
}
MyInt(MyInt const &b) {
std::cout << "MyInt(MyInt const &b)"<< " @ "<< this << std:
:endl;
}
~MyInt() {
std::cout << "~MyInt()"<< " @ "<< this << std::endl;
}
};
int main()
{
MyInt b = 10;
std::cout << "b @ " << &b << std::endl;
return 0;
}
MyInt(int a) @ 0x22ff1f
MyInt(MyInt const &b) @ 0x22ff1e
~MyInt() @ 0x22ff1f
b @ 0x22ff1e
~MyInt() @ 0x22ff1e
lenovo@lenovo-THINK /c/Users/lenovo/Documents/Visual Studio 2012/Projects/demo/d
emo
$ cat main.cpp; g++ --no-elide-constructors main.cpp -o demo.exe; ./demo.exe
#include <iostream>
class MyInt{
public:
MyInt() {
std::cout << "MyInt()"<< " @ "<< this << std::endl;
}
MyInt(int a) {
std::cout << "MyInt(int a)"<< " @ "<< this << std::endl;
}
MyInt& operator = (MyInt const &b) {
std::cout << "MyInt& operator = (MyInt const &b)"<< " @ "<< th
is << std::endl;
return *this;
}
MyInt(MyInt const &b) {
std::cout << "MyInt(MyInt const &b)"<< " @ "<< this << std::en
dl;
}
~MyInt() {
std::cout << "~MyInt()"<< " @ "<< this << std::endl;
}
};
int main()
{
MyInt b = 10;
std::cout << "b @ " << &b << std::endl;
return 0;
}
MyInt(int a) @ 0x22ff1f
MyInt(MyInt const &b) @ 0x22ff1e
~MyInt() @ 0x22ff1f
b @ 0x22ff1e
~MyInt() @ 0x22ff1e
cat main.cpp; g++ --no-elide-constructors main.cpp -o demo.exe; ./demo.exe
#include <iostream>
class MyInt{
public:
MyInt() {
std::cout << "MyInt()"<< " @ "<< this << std::endl;
}
MyInt(int a) {
std::cout << "MyInt(int a)"<< " @ "<< this << std::endl;
}
MyInt& operator = (MyInt const &b) {
std::cout << "MyInt& operator = (MyInt const &b)"<< " @ "<<
this << std::endl;
return *this;
}
MyInt(MyInt const &b) {
std::cout << "MyInt(MyInt const &b)"<< " @ "<< this << std:
:endl;
}
~MyInt() {
std::cout << "~MyInt()"<< " @ "<< this << std::endl;
}
};
int main()
{
MyInt b = 10;
std::cout << "b @ " << &b << std::endl;
return 0;
}
MyInt(int a) @ 0x22ff1f
MyInt(MyInt const &b) @ 0x22ff1e
~MyInt() @ 0x22ff1f
b @ 0x22ff1e
~MyInt() @ 0x22ff1e
lenovo@lenovo-THINK /c/Users/lenovo/Documents/Visual Studio 2012/Projects/demo/d
emo
$ cat main.cpp; g++ --no-elide-constructors main.cpp -o demo.exe; ./demo.exe
#include <iostream>
class MyInt{
public:
MyInt() {
std::cout << "MyInt()"<< " @ "<< this << std::endl;
}
MyInt(int a) {
std::cout << "MyInt(int a)"<< " @ "<< this << std::endl;
}
MyInt& operator = (MyInt const &b) {
std::cout << "MyInt& operator = (MyInt const &b)"<< " @ "<< th
is << std::endl;
return *this;
}
MyInt(MyInt const &b) {
std::cout << "MyInt(MyInt const &b)"<< " @ "<< this << std::en
dl;
}
~MyInt() {
std::cout << "~MyInt()"<< " @ "<< this << std::endl;
}
};
int main()
{
MyInt b = 10;
std::cout << "b @ " << &b << std::endl;
return 0;
}
MyInt(int a) @ 0x22ff1f
MyInt(MyInt const &b) @ 0x22ff1e
~MyInt() @ 0x22ff1f
b @ 0x22ff1e
~MyInt() @ 0x22ff1e
void conv(string &str){}//这句错误
void conv(const string &str){}//这句正确,加上const引用,因为标准规定临时对象是不能更改的,所以要加上const修饰
int main()
{
conv("dasd");//在这里编译器自动生成一个string(“dasd”)临时对象,不能将该临时对象传给非const引用。
}
所以,现在楼主知道什么叫绑定到临时对象了吧。绑定就是建立关联;绑定到临时对象就是将const引用于临时对象string(“dasd”)建立关联