函数重载
c++产生函数符号的时候,由函数名+参数列表类型组成的。
c产生函数符号的时候,由函数名来决定,不支持重载。
定义
函数重载
Tips:一组函数,函数名相同,参数列表也相同,仅仅是返回值不同,这个不叫重载。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| bool compare(int a,int b) { cout<<"compare_int_int"<<endl; return a>b; } bool compare(double a,double b) { cout<<"compare_double_double"<<endl; return a>b; }
bool compare(const char * a,const char * b) { cout<<"compare_char *char *"<<endl; return strcmp(a,b)>0; }
int main() { compare(10, 20); compare(100.0, 20.0); compare("aaa", "ccc"); }
|
作用域
一组函数要称得上重载,一定是处在同一个作用域中的。
作用域概念
1 2 3 4 5 6 7
| int data=10; int main() { int data=20; int a=::data; int b=data; }
|
局部作用域函数声明覆盖全局作用域函数模板
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| bool compare(int a,int b) { cout<<"compare_int_int"<<endl; return a>b; } bool compare(double a,double b) { cout<<"compare_double_double"<<endl; return a>b; }
bool compare(const char * a,const char * b) { cout<<"compare_char *char *"<<endl; return strcmp(a,b)>0; }
int main() { bool compare(int a,int b); compare(10, 20); compare(100.0, 20.0); compare("aaa", "ccc"); }
|
![image]()
修改代码
1 2 3 4 5 6 7
| int main() { bool compare(int a,int b); compare(10, 20); ::compare(100.0, 20.0); ::compare("aaa", "ccc"); }
|
总结:不同作用域的函数是不会发生重载的
形参区别
const(volatile)影响形参类型
1 2 3
| void fun(int a) {}; void fun(const int a) {};
|
int 和const int不算重载;
1 2 3 4
| void fun(int* a) {}; void fun(const int* a) {};
|
打印指针类型
1 2 3 4 5 6 7 8 9 10 11
| int main() { int p = 20; const int* a1 = &p; int* const a2 = &p; int* p_ = &p; cout << typeid(p_).name() << endl; cout << typeid(a1).name() << endl; cout << typeid(a2).name() << endl; return 0; }
|
![image]()
指针和引用
运算符重载
c++运算符重载:使对象的运算表现得和编译器内置类型一样
加法运算符重载
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| class CComplex { public: CComplex(int r=0,int i=0) :mreal(r),mimage(i) {} CComplex operator +(const CComplex &src) { return CComplex(this->mreal + src.mreal, this->mimage + src.mimage); } void show() { cout << mreal <<"+" << mimage<<"j"<<endl; } private: int mreal; int mimage; }; int main() { CComplex comp1(10, 20); CComplex comp2(20, 20); CComplex comp3 = comp1 + comp2; comp3.show(); CComplex comp4 = comp1 + 20; comp4.show(); }
|
全局作用域加法运算符重载
1 2
| CComplex comp5 = 30+comp2; comp5.show();
|
编译器不知道其需要将30转换成复数类型,报错(没有找到接受“CComplex”类型的全局运算符(或没有可接受的转换))
因为30是常量,不能调用作为复数类的成员方法;并且因为30在左边,编译器不知道将30转换成复数类对象(如int a=10+20,这种也不能遇到+号就转换成复数类对象)
1.编译器做对象运算的时候,优先找对象的运算符重载函数;
2.如果没找到,就在全局作用域(::)中找;
3.如果全局作用域没找到,报错
1 2 3 4 5 6 7 8 9 10 11 12 13
| class CComplex {
private: friend CComplex operator +(const CComplex& l, const CComplex& r); };
CComplex operator+ (const CComplex& l, const CComplex& r) { return CComplex(l.mreal + r.mreal, l.mimage + r.mimage); }
|
自增运算符重载
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| CComplex operator++(int) { CComplex comp = *this; mreal += 1; mimage += 1; return comp; }
CComplex& operator++() { mreal += 1; mimage+= 1; return *this; }
|
复合运算符(+=)重载
1 2 3 4 5
| void operator +=(const CComplex& src) { this->mreal += src.mreal; this->mimage += src.mimage; }
|
输入输出流运算符重载
输出流重载,如实现cout<<comp1<<endl;
因为cout不是CComplex类,所以需要在全局作用域中声明
1 2 3 4 5 6 7 8 9 10 11 12
| ostream& operator<<(ostream& out, const CComplex& src) { out << src.mreal << "+" << src.mimage << "j" << endl; return out; }
istream& operator>>(istream& in, CComplex& src) { in >> src.mreal >> src.mimage; return in; }
|
$share-item-width = 1.8rem
.post-share-container {
flex-shrink 0
.share-list-wrap {
display flex
justify-content flex-end
.share-item {
width $share-item-width
height $share-item-width
margin-left 0.5rem
padding 0.4rem
border-style solid
border-width 0.1rem
border-radius 50%
cursor pointer
transition-t("background", "0", "0.3", "ease")
i {
color inherit
font-size 1rem
}
&.qq {
color var(--keep-primary-color)
border-color var(--keep-primary-color)
&:hover {
color var(--background-color-1)
background var(--keep-primary-color)
}
}
&.wechat {
color var(--keep-success-color)
border-color var(--keep-success-color)
img {
filter brightness(1) !important
&[lazyload] {
&::before {
background #fff !important
}
}
}
&:hover {
color var(--background-color-1)
background var(--keep-success-color)
}
}
&.weibo {
color var(--keep-danger-color)
border-color var(--keep-danger-color)
&:hover {
color var(--background-color-1)
background var(--keep-danger-color)
}
}
}
}
}
if (hexo-config('comment') && hexo-config('comment.enable') == true && hexo-config('comment.use')) {
if (hexo-config('comment.use') == "valine") {
@import "./valine.styl"
}
else if (hexo-config('comment.use') == "gitalk") {
@import "./gitalk.styl"
}
else if (hexo-config('comment.use') == "twikoo") {
@import "./twikoo.styl"
}
else if (hexo-config('comment.use') == "waline") {
@import "./waline.styl"
}
}
.comments-container {
display inline-block
width 100%
margin-top var(--component-gap)
.comment-area-title {
width 100%
color var(--text-color-3)
font-size 1.38rem
line-height 2
i {
color var(--text-color-3)
}
+keep-tablet() {
font-size 1.2rem
}
}
.configuration-items-error-tip {
display flex
align-items center
margin-top 1rem
color var(--text-color-3)
font-size 1rem
i {
margin-right 0.3rem
color var(--text-color-3)
font-size 1.2rem
}
}
.comment-plugin-fail {
display none
flex-direction column
align-items center
justify-content space-around
width 100%
padding 2rem
.fail-tip {
color var(--text-color-3)
font-size 1.1rem
}
.reload {
margin-top 1rem
}
}
.comment-plugin-loading {
flex-direction column
padding 1rem
color var(--text-color-3)
.loading-icon {
color var(--text-color-4)
font-size 2rem
}
.load-tip {
margin-top 1rem
color var(--text-color-4)
font-size 1.1rem
}
}
}