[英]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.hpp
和foo.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.hpp
的Foo
类定义中。 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.