简体   繁体   English

将类声明与定义分开

[英]Separate class declaration from definition

I would like to separate class definition and declaration into 2 separate files: foo.hpp and foo.inl .我想将类定义和声明分成 2 个单独的文件: foo.hppfoo.inl

foo.hpp file has Foo class declaration with its' description, and also this file includes foo.inl : foo.hpp文件具有Foo类声明及其描述,并且该文件还包括foo.inl

/* foo.hpp */

// Foo class description comment
class Foo;

#include "foo.inl"

foo.inl contains Foo definition without any code-description comments. foo.inl包含Foo定义,没有任何代码描述注释。

/* foo.inl */

class Foo {
    Foo() = default;

    void bar() {
        /* do something */
    }
}

I am trying to write commentaries for Foo 's methods in foo.hpp to make it look like this:我正在尝试为foo.hpp Foo方法编写注释,使其看起来像这样:

/* foo.hpp */

// Foo class description comment
class Foo;

// This is my default constructor
Foo::Foo();

// This is my very helpful function
Foo::bar();

#include "foo.inl"

But compiler gives an understandable error: invalid use of incomplete type 'class Foo' .但是编译器给出了一个可以理解的错误: invalid use of incomplete type 'class Foo'

So is there any way how can I declare functions and write comments for them in such a way?那么有什么方法可以以这种方式声明函数并为它们编写注释?

Foo::Foo(); and void Foo::bar();void Foo::bar(); area member function declarations, and those are not allowed outside of a class.区域成员函数声明,并且不允许在类之外使用。

You have it a little bit backward, define the class in the "main" header file foo.hpp .你有点落后,在“主”头文件foo.hpp定义类。 Then define the functions as inline in the "inline" header file foo.inl .然后在“内联”头文件foo.inl函数定义为inline联。

Perhaps something like this:也许是这样的:

// Foo.hpp
#pragma once

class Foo
{
public:
    // The default constructor
    Foo();

    // This is my very helpful function
    void bar();
};

#include "Foo.inl"

Then the inline file:然后是内联文件:

// Foo.inl
#pragma once

inline Foo::Foo()
{
}

inline void Foo::bar()
{
}

If the inline functions are simple enough, put them inside the Foo class definition in the main header file Foo.hpp instead.如果内联函数足够简单,请将它们放在主头文件Foo.hppFoo类定义中。 And if they're too complicated to really be inline, put them in a separate source file Foo.cpp to be built with your application (but not included with #include ).如果它们太复杂而无法真正内联,请将它们放在单独的源文件Foo.cpp ,以便与您的应用程序一起构建(但包含在#include )。

If you want to split definition and declaration of class methods, you have to define the class:如果要拆分类方法的定义和声明,则必须定义类:

// .h

// That is my class Foo
class Foo {
    // Constructor
    Foo();

    // This is my very helpful function
    void bar();
};

and

// cpp
Foo::Foo() = default;

void Foo::bar() {
    /* do something */
}

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

相关问题 如何分离子模板类的定义和声明 - How to separate definition and declaration of child template class 如果不输入别名类型的完整声明,则无法从单独文件中的类模板定义访问类型别名 - Cannot access type alias from class template definition in separate file without typing full declaration of alias type 如何使用库中的C ++ 11中的'extern'将定义与类模板的声明分开(dll,so,..) - How to separate definition from the declaration for a class template using 'extern' in C++11 in a library (dll, so,..) 为什么在C ++中将类声明和定义放在两个单独的文件中? - Why put a class declaration and definition in two separate files in C++? C++ - 模板 class 中模板 function 的单独声明/定义 - C++ - Separate declaration/definition for template function in template class 构造函数定义中类声明的模板值 - Template value from class declaration in constructor definition 类定义和类声明 - class definition and class declaration 在类定义中使用声明 - using declaration in class definition 将内联类成员函数的声明和定义分别分配到不同的文件(标头和源文件) - Separate inline class member function declaration and definition to different files (header and source) 是否可以将内联函数的声明和定义分开? - Is possible to separate declaration and definition of inline functions?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM