问题描述
背景
我有一个抽象类,如
class IConverter{
public:
virtual void DoConvertion() = 0;
};
将会有很多具体的类实现 DoConvertion p>
There will be many concrete classes which just implements DoConvertion method.
class TextConverter : public IConverter{
public:
virtual void DoConvertion(){
// my code goes here
}
};
class ImageConverter : public IConverter{
public:
virtual void DoConvertion(){
// my code goes here
}
};
这样会有很多具体的实现。我创建了一个头文件,例如,具有抽象类 IConverter 的CharacterConverter.h 。
There will be many concrete implementation like this. I have created a header file say, CharacterConverter.h which has the abstract class IConverter.
问题
由于我的具体类只是实现了 DoConvertion 方法,是否需要为每个具体类创建单独的头文件?我的意思是,它需要为所有具体类创建 ImageConverter.h , TextConverter.h 等等?所有这些头文件将包含与 IConverter 抽象类相同的代码。
Since my concrete classes just implement the DoConvertion method, is it required to create separate header files for each concrete class? I mean is it required to create ImageConverter.h, TextConverter.h and so on for all concrete classes? All these header files is going to contain the same code like IConverter abstract class.
任何想法?
推荐答案
你的设计的其余部分,是一个工厂,其中你的抽象类有一个静态方法(或多个静态方法,取决于如何实现它),构造适当的子类,并将其作为IConverter *返回。通过这个,你可以只暴露头文件中的抽象定义,并在所有的具体类定义和实现在一个单一的.cpp文件以及超类实现。如果你的子类很大,这会有点笨拙,但是使用较小的类会减少你必须管理的文件数量。
Something you might consider, depending on the rest of your design, is a factory, where your abstract class has a static method (or multiple static methods, depending on how you implement it) that constructs the appropriate subclass and returns it as an IConverter*. With this, you can expose only the abstract definition in the header file, and have all the concrete class definitions and implementations in a single .cpp file along with the super class implementation. This gets a bit unwieldy if your subclass are large, but with smaller classes it reduces the number of files you have to manage.
但是,正如其他人所指出的,最终是一个判断的呼唤。唯一的性能问题将涉及编译;更多的cpp文件可能需要(稍长)编译和更多的头文件可能增加依赖性分析。但是没有要求每个头文件都有一个匹配的cpp,反之亦然。
But, as others have pointed out, it's ultimately a judgment call. The only performance issues would be related to compiling; more cpp files might take (slightly) longer to compile and more header files might increase dependency analysis. But there's no requirement that every header file have a matching cpp and vice verse.
根据注释,我建议这样的结构:
Based on the comments, I'd recommend a structure like this:
IConverter.h ==> IConverter的定义
Converters.h ==>所有子类的定义
IConverter.cpp == > include IConverter.h和Converters.h,包含IConverter抽象功能(静态工厂方法和任何可继承的功能)的实现
TextConvter.cpp,ImagerConverter.cpp等==>单独的cpp文件每个子类,每个包含IConverter.h和Converters.h
IConverter.h ==> definition of IConverter
Converters.h ==> definitions of all subclasses
IConverter.cpp ==> include IConverter.h and Converters.h, contain implementation of IConverter abstract functionality (static factory method and any inheritable functionality)
TextConvter.cpp, ImagerConverter.cpp, etc. ==> seperate cpp files for each subclass, each containing IConverter.h and Converters.h
这允许您只在任何使用工厂和通用功能的客户端中包含IConverter.h。将所有其他定义放在一个标题中,您可以合并,如果他们都基本相同。单独的cpp文件允许您利用Brian提到的编译器优势。你可以内联头文件中的子类定义,如上所述,但这不会真的买你任何东西。你的编译器通常比你在内联优化时更聪明。
This allows you to only include the IConverter.h in any clients that use the factory and generic functionality. Putting all the other definitions in a single header allows you to consolidate if they're all basically the same. Separate cpp files allow you to take advantage of the compiler benefits mentioned by Brian. You could inline the subclass definitions in header files as mentioned, but that doesn't really buy you anything. Your compiler is usually smarter than you are when it comes to optimizations like inline.
这篇关于为具体类分隔头文件 - C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!