避免在类头文件中声明私有函数 (C++)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5665471/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:38:38  来源:igfitidea点击:

Avoiding declaring private functions in class header files (C++)

c++functionheaderprivatedeclare

提问by user664303

(In C++) I have a class whose structure is declared in a header file. That header file is included in lots of source files, such that when I edit it I need to recompile lots of files.

(在 C++ 中)我有一个类,其结构在头文件中声明。该头文件包含在许多源文件中,因此当我编辑它时,我需要重新编译许多文件。

The class has a set of private functions which are only called in one source file. Currently they are declared in the class structure in the header file. When I add a new function of this type, or edit the arguments, it therefore causes recompilation of lots of files. I would like to declare the functions somewhere else, such that only the file that defines and calls them is recompiled (to save time). They still need to be able to access the internal class variables, though.

该类具有一组仅在一个源文件中调用的私有函数。目前它们是在头文件的类结构中声明的。当我添加这种类型的新函数或编辑参数时,会导致重新编译大量文件。我想在其他地方声明这些函数,以便只重新编译定义和调用它们的文件(以节省时间)。不过,他们仍然需要能够访问内部类变量。

How can I achieve this?

我怎样才能做到这一点?

采纳答案by user664303

There is no way to declare member functions of a class outside the main class declaration. So, if you want to declare, outside of the class in question, functions that can access member variables of a particular instance of the class, then I see no alternative but to pass that instance to the function. Furthermore, if you want the functions to be able to access the private and protected variables you will need to put them in a new class and make the original class a friend of that. E.g.

无法在主类声明之外声明类的成员函数。因此,如果您想在所讨论的类之外声明可以访问该类特定实例的成员变量的函数,那么我认为别无选择,只能将该实例传递给该函数。此外,如果您希望函数能够访问私有变量和受保护变量,您需要将它们放在一个新类中,并使原始类成为它的朋友。例如

header.h:

标题.h:

class FooImpl;

class Foo {
public:
   int bar();
   friend class FooImpl;
private:
   int var;
}

impl.cpp:

实现.cpp:

#include "header.h"

class FooImpl {
public:
   int bar(Foo &);
}

int FooImpl::bar(Foo &foo) {
return foo.var;
}

int Foo::bar() {
return FooImpl::bar(*this);
}

回答by Erik

Use the pImpl idiom- Your visible class keeps a pointer to the realclass and forwards calls to public member functions.

使用pImpl 习惯用法- 您的可见类保留一个指向真实类的指针,并将调用转发到公共成员函数。

EDIT: In response to comments

编辑:回应评论

// Foo.h:

class FooImpl; // Do *not* include FooImpl.h
class Foo {
public:
  Foo();
  ~Foo();
  //.. also need copy ctor and op=
  int bar();
private:
  FooImpl * Impl;
};

// FooImpl.h:

class FooImpl {
public:
  int bar() { return Bar; }
private:
  int Bar;
};

// Foo.cpp:

#include "FooImpl.h"

Foo::Foo() { Impl = new FooImpl(); }
Foo::~Foo() { delete Impl; }
int Foo::bar() { return Impl->bar(); }

Keep the actual implementation of your class in FooImpl- Fooshould have copies of the publicmembers of FooImpland simply forward calls to these. All users will include only "Foo.h" - you can change all the private details of FooImplwithout the users of Fooseeing any changes.

保持你的类的实际实现FooImpl-Foo应该有公共成员的副本,FooImpl并简单地将调用转发给这些成员。所有用户将只包括“Foo.h” - 您可以更改所有私人详细信息,FooImpl而用户Foo看不到任何更改。

回答by Nikolai Fetissov

Are you looking for Compiler Firewall, a.k.a. PIMPL?

您在寻找Compiler Firewall,又名 PIMPL 吗?

回答by Neil Kirk

Create an abstract base class which contains only the public functions and reference this in your headers. Create your real class as an implementation somewhere else. Only source files which need to create your class need to see the implementation class header.

创建一个仅包含公共函数的抽象基类,并在您的头文件中引用它。创建您的真实类作为其他地方的实现。只有需要创建类的源文件才需要查看实现类头。