C++
今天看了2节侯捷老师得视频&&看完了c++ primer得第二章
总结如下
#ifndef _COMPLEX_
#define _COMPLEX
class comple{
public:
comple (double r=0,double x=0) : re(r),ch(x) {}
double getre() {
return re;
}
double getch (){
return ch;
}
comple& operator += (const comple& x){
return _doapl(this,x);
}
private :
double re,ch;
friend comple& _doapl(comple*ths,const comple& tmp){
ths->re+=tmp.re;
ths->ch+=tmp.ch;
return *ths;
}
};
/*
comple&
comple:: operator +=(const comple &x){
//this->re+=x.re;
//this->ch+=x.ch;
//return *this;
return _doapl(this,x);
}
*/
inline ostream&
operator << (ostream &a,comple& x){
return a<< '(' << x.getre() << x.getch()<<')';
}
comple
operator + ( comple&a, comple&b){
// return comple()
return comple(a.getre()+b.getre(),b.getch()+a.getch());
/// 临时对象
}
#endif
int main() {
comple a,b(1,1);
comple c=a+b;
// cout<<c.getre();
cout<<a<<' '<<b<<' '<<c<<endl;
return 0;
}
代码是侯捷老师讲得。真的是太经典了。

谁调用这个函数谁就是隐藏函数,this

创建临时对象 return by value
c++ primer 总结如下

constexper 能把指针变成一个顶层const constexper int *p = &r 表示得是 指向r得常量指针

typedef 我觉得需要注意得就是
typedef double * p;
const p a = null;
这里得a不能理解成 const doulbe * a;
应该是 一个常量指针 const 修饰得是 double * 这个整体





我身后空无一人,我怎敢倒下

浙公网安备 33010602011771号