我们将非成员函数放在C ++中的哪个文件中? [英] In which file do we put non-member function in C++?

查看:78
本文介绍了我们将非成员函数放在C ++中的哪个文件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于C ++中的非成员函数,通常的做法是什么?我们是将它们放在main.cpp还是头文件或类实现文件中,还是为它制作一个单独的.cpp文件?如果通常的做法是制作一个单独的文件,那么我们应该将非成员函数标头(原型)放在哪里?它是只进入main.cpp还是同时进入两者?

What is the normal practice when it comes to non-member function in C++? Do we put them in main.cpp or header file or class implementation file, or do we make a separate .cpp file for it? If the normal practice is to make a separate file, then where do we put the non-member function header(prototype)? Does it only go in main.cpp or in both of them?

推荐答案

我想说,您不应将非成员函数与类,成员函数和其他符号区别对待.

I would say you should not treat non-member functions differently to classes and member functions and other symbols.

您应该为每个<应用程序的strong>逻辑组件(模块).

You should create a distinct header file .h and a corresponding source file .cpp for each logical component (module) of your application.

所有公共符号应在头文件中声明/定义(无论它们是否是非成员函数),并且所有非公共符号和所有必需的定义都应放在中源文件.

All public symbols should be declared/defined in the header file (whether they be non-member functions or otherwise) and all non-public symbols and all required definitions should go in the source file.

简而言之,根据逻辑程序组件进行分组,而不是根据符号/功能的类型进行分组.

In short, group according to logical program components, rather than by the type of symbol/function.

这篇关于我们将非成员函数放在C ++中的哪个文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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