简体   繁体   English

我们将非成员函数放在C ++的哪个文件中?

[英]In which file do we put non-member function in C++?

What is the normal practice when it comes to non-member function in C++? 关于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? 是将它们放在main.cpp还是头文件或类实现文件中,还是为它制作一个单独的.cpp文件? 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? 它只进入main.cpp还是同时进入两者?

I would say you should not treat non-member functions differently to classes and member functions and other symbols. 我要说的是,您不应将非成员函数与类,成员函数和其他符号区别对待。

You should create a distinct header file .h and a corresponding source file .cpp for each logical component (module) of your application. 您应该为应用程序的每个逻辑组件 (模块)创建一个不同的头文件 .h和一个相应的源文件 .cpp

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. 简而言之,根据逻辑程序组件进行分组,而不是根据符号/功能的类型进行分组。

Your class should have its own .cpp file. 您的班级应该有自己的.cpp文件。 Non-member functions should go in other files (all together or grouped according to similarity). 非成员函数应放在其他文件中(全部或根据相似性分组)。 That's the convention here in North America, but conventions differ. 这是北美的惯例,但惯例有所不同。 Prototype just needs go into header file so you can include it wherever you use it. 原型只需要放在头文件中,这样您就可以在使用它的任何地方包括它。

General idea in pseudo code: 伪代码的一般思路:

if (it will be used in other cpp files) 
    put the declaration in a header file. 
    implement it in a header or a cpp file.
else 
    if (only need it in some functions in a header file)
        if (it's a function more than N line )  // please define this N in your mind
            declare it in the same header and implement it in a cpp file
        else
             put it in the same header file
    else // used in cpp only
        put it in the cpp file

As long as it compiles, you should consider readability (easy for anyone to read) and accessibility (easy for anyone to find and debug). 只要可以编译,就应该考虑可读性 (任何人都容易阅读)和可访问性 (任何人都容易找到和调试)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 How do I call a member function inside a non-member function in C++ with an object created outside the non-member function? - How do I call a member function inside a non-member function in C++ with an object created outside the non-member function? C ++非成员函数,使用引用另一个类的类 - c++ non-member function using a class which references another class 在C ++中如何继承父类的非成员函数,这只是在文件中定义的? - In C++ how to inherit a parent class' non-member functions, which is simply defined in the file? 我可以在C ++中声明一个非成员函数const吗? - Can I declare a non-member function const in C++? C ++:非成员函数的定义/声明的位置 - C++: location of definition/declaration of non-member function C ++ Postfix /前缀运算符重载为非成员函数 - c++ postfix / prefix operator overload as non-member function 来自非成员函数的C ++虚拟继承 - C++ Virtual Inheritance from a non-member function C++ “在非成员函数中无效使用‘this’”, - C++ “Invalid use of 'this' in non-member function”, 按位重载和C ++作为非成员函数 - Overloading bitwise & c++ as non-member function C ++ substr方法-“在非成员函数中无效使用'this'” - C++ substr method - “invalid use of ‘this’ in non-member function”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM