多源目录中的转发声明;模板即时

时间:2021-02-05 06:57:49

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 ?

我正在寻找一本好书,参考资料来处理类esp的前向声明。当源在多个目录中时,例如。 dirA中的A类在dirB中的B类中向前声明?这是怎么做到的?

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

此外,模板问题,高级用途和即时问题的任何材料,高度适用?

Thanks.

3 个解决方案

#1


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.

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

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

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

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.

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

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.

std :: vector testvector将无法编译。

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.

我不知道是否有关于这个主题的书,但你可以在c ++ faq lite上看到这一节。

#2


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).

John Lakos的大规模C ++软件设计(这里的书评)讨论了物理设计(文件)和逻辑设计以及它们与软件组件的关系(它们与类的关系并不总是1:1)。

#3


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目录显示为这个目录,你可以通过调用添加标题

#include "dirB/B.h"

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

因为你会在标题上进行前向删除,所以不会有问题。

#1


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.

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

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

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

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.

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

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.

std :: vector testvector将无法编译。

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.

我不知道是否有关于这个主题的书,但你可以在c ++ faq lite上看到这一节。

#2


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).

John Lakos的大规模C ++软件设计(这里的书评)讨论了物理设计(文件)和逻辑设计以及它们与软件组件的关系(它们与类的关系并不总是1:1)。

#3


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目录显示为这个目录,你可以通过调用添加标题

#include "dirB/B.h"

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

因为你会在标题上进行前向删除,所以不会有问题。