简体   繁体   English

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

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

(In C++) I have a class whose structure is declared in a header file. (在C ++中)我有一个类,其结构在头文件中声明。 That header file is included in lots of source files, such that when I edit it I need to recompile lots of files. 该头文件包含在许多源文件中,因此当我编辑它时,我需要重新编译大量文件。

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? 我怎样才能做到这一点?

Use the pImpl idiom - Your visible class keeps a pointer to the real class 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 - Foo should have copies of the public members of FooImpl and simply forward calls to these. 保持你的类的实际实现在FooImpl - Foo应该有FooImpl公共成员的副本,并简单地转发对这些的调用。 All users will include only "Foo.h" - you can change all the private details of FooImpl without the users of Foo seeing any changes. 所有用户将只包含“Foo.h” - 您可以更改FooImpl的所有私人详细信息,而不FooImpl Foo的用户看到任何更改。

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. 此外,如果您希望函数能够访问私有变量和受保护变量,则需要将它们放在新类中,并使原始类成为其中的朋友。 Eg 例如

header.h: header.h:

class FooImpl;

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

impl.cpp: impl.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);
}

你在寻找编译器防火墙 ,又名PIMPL?

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. 只有需要创建类的源文件才需要查看实现类头。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM