c++类和静态
我有一个关于课程设计的问题。我想要一个封装了一堆相关函数的类。例如一个统计类,它有静态函数来计算算术平均值、经验方差等。或者一个物理类,它可以计算抛射运动、波动等。
你如何在 main 中引用这些函数?参考统计示例,在 Java 中我会执行 Statistics.calcMean()
。
I have a question regarding class design. I want to have a class that encapsulates a bunch of related functions. For instance a Statistics class that would have static functions to calculate the arithmetic mean, empirical variance, etc. Or a physics class that would calculate projectile motion, wave-motion, etc.
How do you reference these function in main? Referring to the statistics example, in Java I would do Statistics.calcMean()
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
统计::calcMean()
。您还可以使用命名空间而不是静态类成员。Statistics::calcMean()
. You could also use a namespace instead of static class members.如果不同的函数之间根本没有共享状态,那么最好不要创建类,而是创建命名空间:
然后从 main 调用它:
If there is no shared state at all among the different functions, it might be a better idea not to create a class but rather a namespace:
And then you call it from main:
使用
::
引用静态类成员:Statistics::calcMean()
Use
::
to reference static class members:Statistics::calcMean()
将静态方法定义为:
调用方式:
Define the static methods as:
Invoke by: