64,654
社区成员
发帖
与我相关
我的任务
分享
int main()
{
int a =2;
int &b =a;
int &c =b;
cout<<"&a=="<<&a<<", &b=="<<&b<<", &c=="<<&c<<endl;
cout<<"a=="<<a<<", b=="<<b<<", &c=="<<c<<endl;
b=4;
cout<<"a=="<<a<<", b=="<<b<<", &c=="<<c<<endl;
return 0;
}
/*
运行结果如下
&a==0x22fee0, &b==0x22fee0, &c==0x22fee0
a==2, b==2, &c==2
&a==0x22fee0, &b==0x22fee0, &c==0x22fee0
a==4, b==4, &c==4
*/
#include <vector>
#include <iostream>
using namespace std;
struct Point {
Point(int xx, int zz)
:a(xx),
ra(xx) {
cout<<"***in Point::Point** "<<endl<<"ra=="<<ra<<", a=="<<a <<endl;
cout<<"&ra=="<<&ra<<", "<<"&xx=="<<&xx<<endl;
}
//赋值操作符重载
Point& operator=(const Point &other) {
cout<<"hello"<<endl;
return *this;
}
int a;
int &ra;
};
int main() {
std::vector<Point> pts;
Point temp(2, 5);
Point temp2(temp);
pts.push_back( temp );
cout<<endl<<"***in main***"<<endl;
cout<<"pts[0].ra == "<<pts[0].ra<<", "<<"& pts[0].ra=="<<&(pts[0].ra)<<endl;
cout<<"temp.ra=="<<temp.ra<<", "<<"& temp.ra=="<<&(temp.ra)<<endl;
cout<<"temp2.ra=="<<temp2.ra<<", "<<"& temp2.ra=="<<&(temp2.ra)<<endl;
temp.ra = 88 ; //改变temp.ra的值
cout<<endl;
cout<<"pts[0].ra == "<<pts[0].ra<<", "<<"& pts[0].ra=="<<&(pts[0].ra)<<endl;
cout<<"temp.ra=="<<temp.ra<<", "<<"& temp.ra=="<<&(temp.ra)<<endl;
cout<<"temp2.ra=="<<temp2.ra<<", "<<"& temp2.ra=="<<&(temp2.ra)<<endl;
return 0;
}
/*
运行结果:
***in Point::Point**
ra==2, a==2
&ra==0x22fe64, &xx==0x22fe64
***in main***
pts[0].ra == 0, & pts[0].ra==0x22fe64
temp.ra==4621588, & temp.ra==0x22fe64
temp2.ra==4621588, & temp2.ra==0x22fe64
pts[0].ra == 0, & pts[0].ra==0x22fe64
temp.ra==4621588, & temp.ra==0x22fe64
temp2.ra==4621588, & temp2.ra==0x22fe64
*/
Point(int xx, int zz)
:a(xx),
ra(xx)
23.1.2/table 68
a.push_back(x) void a.insert(a.end(),x) vector, list, deque
23.1.1/table 67
a.insert(p,t) iterator inserts a copy of t before p.
21.1/table 64
expression return type post-condition
t = u T& t is equivalent to u
if (temp.ra == temp2.ra && temp.ra == pts[0].ra)
{
cout << "all same" << endl;
}
Point temp2(temp);
std::vector<Point> pts;