简体   繁体   English

在类的cpp文件之外实现C ++类函数是一个好主意吗?

[英]Is it a good idea to implement C++ class functions outside of the class' cpp file?

Let's say I have a main.cpp, class1.cpp, class1.h, as well as other header files that won't be specified: 假设我有一个main.cpp,class1.cpp,class1.h以及其他未指定的头文件:

class1.h class1.h

#pragma once

class Class1 {
public:
  Class1(int val);
  void pubFunc();
private:
  int val1;
  void privFunc();
}

class1.cpp class1.cpp

#include "class1.h"

Class1::Class1(int val) {
  val1 = val;
}

void Class1:pubFunc() {
  if (val1 > 0)
    privFunc();
}

main.cpp main.cpp中

#include "class1.h"
#include "anotherclass.h"

AnotherClass ac;

int main() {
  Class1 c1(10);
  c1.pubFunc();
}

Class1::privFunc() {
  ac.doSomething;
}

I want Class1::pubFunc() to essentially trigger another function that can make use of AnotherClass , without including its header inside class1.cpp . 我希望Class1::pubFunc()本质上触发另一个可以利用AnotherClass ,而不将其标头包含在class1.cpp中 This is the method that I thought of, but it feels disorganized, and I'm not too keen on putting the class instances outside of main() . 这是我想到的方法,但是感觉杂乱无章,而且我不太热衷于将类实例放在main() The only other way I can think of doing this making Class1::pubFunc() return a string , which maps to a function, but I don't know if that's any better. 我可以想到的另一种方法是使Class1::pubFunc()返回一个string ,该string映射到一个函数,但是我不知道这样做是否更好。

Misconception. 误解。 A class in C++ does not need to have its own *.cpp file and there are pragmatical reasons to avoid having lots of tiny *.cpp files (and prefer instead having less *.cpp files, each defining several related classes and functions, so being slightly bigger). C ++中的类不需要拥有自己的 *.cpp文件,并且出于实用的原因,要避免拥有许多微小的*.cpp文件(而宁愿使用较少的*.cpp文件,每个文件都定义了几个相关的类和函数,因此稍大)。

A given translation unit (eg the *.cpp files and all the header files it is #include -ing) can (and usually does) define and declare several class -es and functions. 给定的翻译单元 (例如*.cpp文件和#include -ing的所有头文件 )可以(通常确实)定义和声明几个 class -es和函数。

In practice, you code in C++ by using several standard containers or other standard C++ facilities provided by standard C++ headers , such as smart pointers from <memory> , strings from <string> , vectors from <vector> , sorting functions from <algorithm> and so on. 实际上,您可以使用几个标准容器标准C ++头文件提供的其他标准C ++设施(例如, <memory>智能指针, <string><vector><algorithm>排序函数)使用C ++进行编码等等。 The powerful C++ standard library is one of the reasons to code in C++ (instead of C, for instance). 强大的C ++标准库是使用C ++(例如,代替C)进行编码的原因之一。 So you would #include perhaps several standard headers like <vector> or <map> . 因此,您可能会#include 几个标准标头,例如<vector><map> Standard headers are quite complex: on my Linux machine, #include <vector> generally expands to more than ten thousand of included lines (and that is one of the reasons why C++ code compiles slowly). 标准标头非常复杂:在我的Linux机器上, #include <vector>通常扩展到一万多个包含行(这是C ++代码编译缓慢的原因之一)。 So it is unwise (but possible) to have many small C++ files of a hundred lines each which are including some standard header, because a small yourcode.cpp file containing #include <vector> and two hundreds lines of your C++ code is expanded to much more: the compiler parses about 10000 lines of code (LOC) from <vector> and 200LOC of your code, so 10200 LOC in total. 因此,有很多一百行的小C ++文件(其中每个都包含一些标准标头)是不明智的(但有可能的),因为包含#include <vector>和200行C ++代码的小yourcode.cpp文件被扩展为更多:编译器从<vector>解析大约10000行代码(LOC),并从您的代码中解析200LOC,因此总共解析了10200 LOC。

BTW, you can ask your compiler to give the preprocessed form of your translation unit (with GCC , using g++ -C -E yourcode.cpp > yourcode.ii ) and look (with an editor or a pager) inside that yourcode.ii . 顺便说一句,你可以问你的编译器,让您的翻译单元的预处理形式(与海湾合作委员会 ,利用g++ -C -E yourcode.cpp > yourcode.ii )和看(用一个编辑器或寻呼机),那里面yourcode.ii

(your example is not genuine C++11 code; in practice, it should often use standard containers and some other features -smart pointers, threads, standard algorithms...- of the rich C++ standard library , but it does not) (您的示例不是真正的C ++ 11代码;实际上,它应经常使用标准容器和丰富的C ++标准库的其他一些功能-智能指针,线程,标准算法...,但不是)

So I recommend to have your *.cpp containing at least a thousand or two of your C++ source code lines, and of course they could define several of your related classes (and functions). 因此,我建议您的*.cpp至少包含一千或两个C ++源代码行,并且它们当然可以定义几个相关的类(和函数)。 In some cases, a single C++ class might be implemented in several C++ files (because that class has a lot of methods). 在某些情况下,单个C ++ class可能在多个 C ++文件中实现(因为该类具有很多方法)。

BTW, in your C++ classes, a lot of small functions (including member functions) are inlined and you'll define them in some of your header file. 顺便说一句,在你的C ++类,很多小功能(包括成员函数)都是内联,你会在一些你的头文件中定义它们。

I recommend studying the source code of several C++ free software projects for inspiration. 我建议学习一些C ++ 自由软件项目的源代码以获取启发。 You'll find many of them on github or elsewhere. 您可以在github或其他地方找到许多此类文件。

Your question is a consequence of an earlier questionable decision -- to use a global variable to share an instance of AnotherClass between main() and Class1 . 您的问题是先前有问题的决定的结果-使用全局变量在main()Class1之间共享AnotherClass的实例。

If you break that dependency, perhaps by using a callback, or an interface, then your need to scatter Class1 member function definitions across multiple compilation units will disappear as well. 如果您通过使用回调或接口打破了这种依赖性,那么将Class1成员函数定义分散到多个编译单元中的需求也将消失。

Here's one way you might cure the dependency: 这是解决依赖关系的一种方法:

class1.h class1.h

#include <functional>

class Class1
{
public:
  Class1(int val, std::function<void(void)> callback);
  void pubFunc(void);
private:
  std::function<void(void)> do_the_thing;
  int val1;
};

class1.cpp class1.cpp

#include "class1.h"

Class1::Class1(int val, std::function<void(void)> callback)
   : val1(val), do_the_thing(std::move(callback))
{
}

void Class1:pubFunc(void)
{
  if (val1 > 0)
    do_the_thing();
}

main.cpp main.cpp中

#include "class1.h"
#include "anotherclass.h"

int main(void)
{
  AnotherClass ac;

  Class1 c1(10, std::bind(&AnotherClass::doSomething, ac));
  // can also write as     [](){ ac.doSomething(); }
  c1.pubFunc();
}

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

相关问题 在类外部定义的c ++函数在单独的.cpp文件和makefile链接中 - c++ functions defined outside of class in separate .cpp file and makefile linking c++ 类和 .cpp 文件的单独文件,函数不是类的一部分 - c++ seperate files for class and .cpp file, Functions not part of class c ++中的class(cpp文件和h文件) - class (cpp file & h file) in c++ 将类分为cpp和头文件(C ++) - Separating a class into cpp and header file (C++) 如何在 *.cpp 文件中实现静态类成员函数? - How to implement static class member functions in *.cpp file? 类外的C ++虚函数实现 - C++ virtual functions implementation outside the class 在C ++中的单独的.cpp文件中,以头文件编写的类的成员函数的定义 - Definition of member functions of a class written in a header file, in a separate .cpp file in C++ 将Exception创建为内部类是一个好主意吗? C ++ - Is it a good idea to create Exception as inner class? C++ C ++ :. cpp文件中的实例本身的表示关键字定义了类的功能 - C++: Representation keyword of the instance itself inside a .cpp file defining functions of a class Cpp/ C++ unique Pointer on object access functions of that class - Cpp/ C++ unique Pointer on objects access functions of that class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM