📄 automaticoperatorequals.cpp
字号:
//: C12:AutomaticOperatorEquals.cpp
// From Thinking in C++, 2nd Edition
// Available at http://www.BruceEckel.com
// (c) Bruce Eckel 2000
// Copyright notice in Copyright.txt
#include <iostream>
#include <string>
using namespace std;
class Cargo {
public:
Cargo& operator=(const Cargo&) {
cout << "inside Cargo::operator=()" << endl;
return *this;
}
};
class Truck {
private:
Cargo b;
char* p;
int i;
/*
public:
Truck& operator=(const Truck& t)
{
//释放当前对象有关的成员
delete p;
p = new char[...]
strcpy(p,t.p);
//当前对象有关的成员=t.相应成员
i = t.i;
b = t.b;
return *this;
}
*/
};
class parent{
public:
parent& operator=(const parent& com)
{
cout<<"inside parent::operator=()"<<endl;
return *this;
}
};
class child:public parent{
};
class third{
child c;
char* p;
public:
third& operator=(const third& t)
{
delete p;
p = new char[::strlen(t.p)+1];
::strcpy(p,t.p);
c = t.c;
return *this;
}
};
void main() {
Truck a, b;
a = b; // Prints: "inside Cargo::operator=()"
// child m,n;
// m=n;
} ///:~
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -