函数与运算符重载
Qingyh Lv3

函数重载

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; //main函数内的局部作用域
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由全局作用域变成了局部作用域,即局部作用域只有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由全局作用域变成了局部作用域,即局部作用域只有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) {};
//这里的const没有起作用,两个函数本质上都是void fun(int a)

int 和const int不算重载;

1
2
3
4
void fun(int* a) {};
void fun(const int* a) {};
//是重载,这是因为const在参与类型判断的时候只看其右边有没有指针,有的话const起作用,否则不起作用。
//分别是void fun(int* )和void fun(int const* )的重载

打印指针类型

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类对象的加法操作
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; //即comp1.operator(comp2)
comp3.show();
//优先调用成员方法运算符+
CComplex comp4 = comp1 + 20; //即comp1.operator(CComplex(20,0))
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:
//在CComplex中定义为友元函数,这样能够访问私有变量
friend CComplex operator +(const CComplex& l, const CComplex& r);
};
//全局作用域内定义
//这个是重载了一个全局的+运算符,CComplex comp5 = 30+comp2;
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
//同样需要在CComplex类中声明友元函数,以访问成员变量
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 } } }
由 Hexo 驱动 & 主题 Keep
本站由 提供部署服务
总字数 42.9k
$li-margin-bottom = 0.8rem $post-tool-button-width = 2.5rem .post-tools-container { padding-top var(--component-gap) .post-tools-list { li { margin-bottom $li-margin-bottom &:last-child { margin-bottom 0 } } li.tools-item { position relative box-sizing border-box width $post-tool-button-width height $post-tool-button-width color var(--text-color-3) font-size 1.2rem background var(--background-color-1) border-radius 50% box-shadow 2px 2px 5px var(--shadow-color) cursor pointer &:hover { box-shadow 2px 2px 8px var(--shadow-hover-color) } i { color var(--text-color-3) } &:hover { color var(--background-color-1) background var(--primary-color) i { color var(--background-color-1) !important } } &.toggle-show-toc { display none } &.go-to-comments { .post-comments-count { position absolute top 0 right -1rem display none align-items center justify-content center box-sizing border-box min-width 1.1rem height 1.1rem padding 0 0.2rem color var(--badge-color) font-size 12px background var(--badge-background-color) border-radius 0.4rem +keep-tablet() { display none !important } } } } li.status-item { width $post-tool-button-width height $post-tool-button-width color var(--text-color-3) font-size 1.6rem cursor pointer &.post-lock { cursor default .fa-lock-open { display none color var(--keep-success-color) } &.decrypt { cursor pointer .fa-lock-open { display block } .fa-lock { display none } } } } } }