简体   繁体   English

C ++通用类-分离接口和实现

[英]C++ generic classes - separating interface and implementation

A very common coding practice is to separate the interface of a class from the implementation of its member functions through the use of .h and .cpp files on a per-class basis. 一种非常常见的编码实践是通过在每个类的基础上使用.h.cpp文件来将类的接口与其成员函数的实现分开。 So class Foo would be realised with a Foo.h header file and a corresponding Foo.cpp file. 因此,将使用Foo.h头文件和相应的Foo.cpp文件来实现class Foo

This is often thrown out of the window in the special case of generic classes and instead header-only libraries are used to keep the compiler happy even though it does clutter the interface file with implementation details. 在泛型类的特殊情况下,这常常被抛在窗外,而是使用仅标头的库来使编译器满意,即使它确实使接口文件杂乱了实现细节。

I've recently come accross some code written as follows. 我最近遇到了一些如下编写的代码。 The .h file contains the interface and a #include to a .hpp file which contains the implementation of the generic member functions. .h文件包含接口和.hpp文件的#include ,该文件包含通用成员函数的实现。

eg for a simple container of type T Value.h 例如,对于类型为T Value.h的简单容器

#ifndef VALUE_H
#define VALUE_H

template <typename T>
class Value
{
public:
    Value(T value);
    void set(T value);
    T get() const;
private:
    T data;
};

#include "Value.hpp"

#endif

and the corresponding Value.hpp 和相应的Value.hpp

#ifndef VALUE_HPP
#define VALUE_HPP

template <typename T>
Value<T>::Value(T value) : data(value)
{
}

template <typename T>
void Value<T>::set(T value)
{
    data = value;
}

template <typename T>
T Value<T>::get() const
{
    return data;
}

#endif

This has the advantage of better separating interface and implementation coupled with the further benefit of actually compiling (in my limited testing). 这具有更好地分离接口和实现的优点,以及实际编译的进一步好处(在我有限的测试中)。

My question is then are there any hidden pit-falls with adopting this convention ? 我的问题是,采用该约定是否存在任何隐患

Since you need to include both "value.h" and "value.hpp" in every file that uses the Value class, there is no benefit in compile time. 由于在使用Value类的每个文件中都需要同时包含“ value.h”和“ value.hpp”,因此编译时没有任何好处。 But if you mean "it compiles" vs a solution where you put the implementation in a .cpp file, then yes, there is a benefit. 但是,如果您的意思是“它可以编译”而不是将实现放在.cpp文件中的解决方案,那么是的,这是有好处的。

There is of course a benefit in that you can easily see the interface(s) provided by the class, without having the file cluttered up with a bunch of implementation code. 当然,这样做的好处是,您可以轻松查看类提供的接口,而不会因一堆实现代码而使文件混乱。

I'm not sure the naming convention of "value.h" and "value.hpp" is the 'best' choice. 我不确定“ value.h”和“ value.hpp”的命名约定是否是“最佳”选择。 I think the "value.inl" is a better name for the second file. 我认为“ value.inl”是第二个文件的更好名称。

No there are no special pitfalls with this solution. 没有这种解决方案没有特别的陷阱。 The .hpp file is just another header file which contains the definitions for the methods declared by the templated class. .hpp文件只是另一个头文件,其中包含模板化类声明的方法的定义。 Since methods of templated classes needs to be defined in the header file this is a convenient method for separating the declaration from the definition. 由于需要在头文件中定义模板化类的方法,因此这是将声明与定义分开的便捷方法。 The extension .hpp shows that it is a hybrid of header and implementation file and is commonly used. 扩展名.hpp表明它是标头和实现文件的混合,并且很常用。

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

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