多个源目录中的前向声明; 模板实例化

发布于 2024-07-28 16:07:42 字数 149 浏览 8 评论 0原文

我正在寻找一本好书,参考材料,涉及类的前向声明,特别是。 当源位于多个目录中时,例如。 dirA 中的 A 类在 dirB 中的 B 类中向前声明? 这是怎么做到的?

另外,有任何有关模板问题、高级用途和实例化问题的材料吗?

谢谢。

I am looking for a nice book, reference material which deals with forward declaration of classes esp. when sources are in multiple directories, eg. class A in dirA is forward declared in class B in dirB ? How is this done ?

Also, any material for template issues, advanced uses and instantation problems, highly appreicated ?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

苏佲洛 2024-08-04 16:09:53

如果它们位于并行目录中,您可以像这样包含它,

#include "../dirB/B.h"

但在标头中,您只需调用此行进行前向减速,

class B;

而不是此行,您可以将包含目录和源目录分开。

因此,您可以将包含目录显示为该目录,并且可以通过调用添加标头,

#include "dirB/B.h"

因为您将在标头上进行前向减速,这不会有问题。

if they are in parallel directories you can include it like

#include "../dirB/B.h"

but in header you just call this line for forward decleration

class B;

instead of this, you can seperate your include directories and source directories.

so then you can show include directory as this directory and you can add header by calling

#include "dirB/B.h"

since you will make a forward decleration on header, it wont be problem.

一绘本一梦想 2024-08-04 16:09:27

一般来说,您可以在标头中转发声明作为避免完全包含的方法或作为启用循环引用的方法(不好)。
您可以仅通过指针或引用或返回类型来使用前向声明类型。

John Lakos 的《大规模 C++ 软件设计》(此处为书评) 解决了物理问题设计(文件)和逻辑设计以及它们与软件组件的关系(与类并不总是 1:1)。

Generally speaking you can forward declare in headers as a means of avoiding full includes or as a way to enable circular referencing (bad).
You can use a forward declared type by pointer or reference or return type only.

Large-Scale C++ Software Design by John Lakos (book review here) addresses physical design (files) and logical design and how they relate to software components (which aren't always 1:1 with classes).

┼── 2024-08-04 16:08:59

前向声明与项目的目录结构无关。 即使项目中不存在,您也可以转发声明。 它们主要用于解决类之间的循环引用,并在不需要完整的类声明时加快编译速度,相应的#include可以用前向声明替换。

要确定前向声明何时足够,sizeof() 查询通常可以回答该问题。 例如,

class Wheel;

class Car
{
    Wheel wheels[4];
};

在此声明中,不能使用前向声明,因为编译器无法确定 Car 的大小:它不知道车轮包含多少数据。 换句话说,sizeof(Car) 是未知的。

另外,关于模板,如果模板类包含模板参数的数据成员(但它们的指针可以),则前向声明的类不能用作模板参数。 例如,

template<class T> class pointer
{
    T *ptr;
};

class Test;
pointer<Test> testpointer;

是合法的,但是

std::vector是合法的。 testvector 将无法编译。

由于上述限制,前向声明的类通常用作指针或引用。

我不知道是否有关于该主题的书,但您可以查看 本节有关 C++ FAQ lite。

Forward declarations have nothing to do with the directory structure of your project. You can forward declare something even not existing in your project. They are mostly used to resolve cyclic references between classes and to speed up compilation when the complete class declaration is not necessary, and the corresponding #include can be replaced with a forward declaration.

To determine when a forward declaration is sufficient, the sizeof() query can usually answer the question. For example,

class Wheel;

class Car
{
    Wheel wheels[4];
};

In this declaration, a forward declaration cannot be used since the compiler cannot determine the size of a Car: it doesn't know how much data the wheels contain. In other words, sizeof(Car) is unknown.

Also regarding templates, forward declared classes cannot be used as template parameters if the template class contains data members of the template parameter (but their pointers can be). For instance,

template<class T> class pointer
{
    T *ptr;
};

class Test;
pointer<Test> testpointer;

is legal but

std::vector<Test> testvector will not compile.

Because of the aforementioned limitations, forward declared classes are generally used as pointers or references.

I don't know if there's a book on the subject but you can see this section on c++ faq lite.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文