C++:非成员函数和静态成员函数的区别? [英] C++: Difference Between Non-Member Function and Static Member Function?

查看:54
本文介绍了C++:非成员函数和静态成员函数的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里有一个简单的问题:静态成员函数(即无需对象访问即可调用的函数(仅使用类标识符)和非成员函数)之间有什么区别?在这里,我从概念上和功能上都提出了要求.

Simple question, here: what is the difference between a static member function, i.e. a function that can be called without requiring an object to access it (simply using the class identifier), and a non-member function? Here, I am asking both conceptually and functionally.

非成员函数在概念上是静态的吗?

Are non-member functions conceptually static?

推荐答案

静态成员函数可以访问类的私有部分和受保护部分.默认情况下,非成员函数不能这样做.只有当一个班级授予他们友谊时,他们才能做到这一点.

static member functions can access private and protected sections of a class. Non-member functions cannot do that as default. They can do that only if a class grants them friendship.

另一点需要考虑的是静态成员函数的名称在类的范围内.多个类可以拥有同名的静态成员函数,而不必担心名称冲突.

Another point to consider is that name of the static member functions are in the scope of the class. Multiple classes can have static member functions with the same name without worrying about names conflicting.

这篇关于C++:非成员函数和静态成员函数的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆