简体   繁体   English

派生类中的相同#include语句

[英]same #include statements in derived classes

I have two questions 我有两个问题

lets say there is a base class and several derived classes, the derived classes are going to have all of the #include statements (like #include <iostream> etc) and using lines of the base class. 假设有一个基类和几个派生类,派生类将具有所有#include语句(如#include <iostream>等)并using基类的行。 1. is it considered a good practice to write the base class's #include statements and using lines in the in the derived classes h. 1.编写基类的#include语句并在派生类h中using行是否被视为一种好习惯。 file anyway? 反正还是文件?

2.same question regarding composition - class A has an object of class B as a member, is it a good practice to writes B s #include` statements in A's h. 2关于构图的相同问题-类A有一个类B的对象作为成员,将B s #include`语句写在A的h中是一种好习惯吗 file anyway? 反正还是文件?

thanks! 谢谢!

I think it is a good practice to have each include file actually #include any of the definitions it needs, so that in your main program, you can #include your most deeply derived class without having to have additional #include statements for the definitions that class needs. 我认为这是一个好习惯,每个include文件实际上#include它需要的任何定义,因此,在您的主程序中,您可以#include最深层次的派生类,而不必为包含以下定义的附加#include语句班级需求。

You can have class definitions contain pointers to previously undefined classes, as follows: 您可以使类定义包含指向以前未定义的类的指针 ,如下所示:

class forward_ref;

class myClass :
Public:
 myClass(){}
 forward_ref* ptr_to_forward_ref;
};

lets say there is a base class and several derived classes, the derived classes are going to have all of the #include statements (like #include <iostream> etc) and using lines of the base class. 假设有一个基类和几个派生类,派生类将具有所有#include语句(如#include <iostream>等)并使用基类的行。

Not at all. 一点也不。

First of all, you are not supposed to put using lines at top-level scope in header files. 首先,您应该在头文件的顶级范围内using行。 This is a common beginners' mistake. 这是一个常见的初学者的错误。

Second, what makes you think the derived class needs all #include s of the base class? 其次,是什么让您认为派生类需要基类的所有#include The derived-class header file needs to include the base-class header file, and the derived-class implementation file needs to include the derived-class header file. 派生类头文件需要包含基类头文件,派生类实现文件需要包含派生类头文件。

This already gives you all includes. 这已经给了您所有的东西。

base.h: base.h:

#include <string>

class Base
{
    // ...
    virtual std::string f(); // no `using`
};

derived.h: h。

#include "base.h"
// no need for <string> here

class Derived : public Base
{
    // ...
    virtual std::string f();
};

derived.cpp: 派生的.cpp:

#include "derived.h"
// no need for <string> here

std::string Derived::f() // could have used `using std::string`
{
    // ...
}

Now of course, it's technically possible to actually do it otherwise, in a more complicated fashion, like this: 当然,现在,从技术上讲 ,实际上可以以更复杂的方式执行此操作,如下所示:

base.h: base.h:

// no <string>?!

class Base
{
    // ...
    virtual std::string f();
};

derived.h: h。

#include "base.h" // still no <string>?!

class Derived : public Base
{
    // ...
    virtual std::string f();
};

derived.cpp: 派生的.cpp:

#include <string> // ah, OK
#include "derived.h"

std::string Derived::f()
{
    // ...
}

This works only because the compiler doesn't separately compile header files but only compilation units (~= .cpp files) as a whole, after all includes have been processed. 这之所以起作用,是因为在处理完所有包含文件之后,编译器不会单独编译头文件,而只会整体编译整个编译单元(〜= .cpp文件)。

But talk about horrible programming style. 但是谈论可怕的编程风格。 Why would you want to force everyone who derives from your class to include extra headers? 您为什么要强迫从您的班级派生的每个人都包括额外的标头?

2.same question regarding composition - class A has an object of class B as a member, is it a good practice to writes Bs#include` statements in A's h. 2.关于组成的相同问题-类A有一个类B的对象作为成员,在A的h中写Bs#include`语句是一种好习惯吗? file anyway? 反正还是文件?

It depends. 这取决于。 If A 's header needs access to any of B 's members, then you have to use an include, and in that case, again, you just include what you need in bh and let ah #include "bh" . 如果A的标头需要访问B的任何成员,则必须使用include,在这种情况下,只需在bh中包含所需的内容,并让ah #include "bh"

If A 's header only needs a pointer or a reference to B , or just a return value, then you can use a forward declaration in order to potentially speed up compilation. 如果A的标头仅需要指向B的指针或引用,或者仅是返回值,则可以使用前向声明以潜在地加快编译速度。 Of course, speeding up compilation is not something a C++ beginner should care about, because a lot of time will pass until you will be developing software which take hours to build :) 当然,加快编译速度并不是C ++初学者应该关心的事情,因为要花很多时间才能开发出需要花费大量时间才能构建的软件,否则将花费很多时间:)

Anyway, for completeness' sake: 无论如何,出于完整性考虑:

ah: 啊:

class B; // forward declaration, no #include

class A
{
    // ...
    B f();
    B *ptr;
    B &ref;
};

a.cpp: a.cpp:

#include "a.h"
#include "b.h" // need the include here

B A::f()
{
    B obj;
    return obj;
}

bh: bh:

class B
{
    // ...
};

In answer to question 2: typically you would include class B's header file in A's header file (so all the includes needed for A will automatically be included). 在回答问题2:通常,您将在A的头文件中包含B类的头文件(因此A所需的所有包含都将自动包含在内)。

However, as pointed out in Logicrat's answer, you could alternatively include only a pointer to B in A. In this case you can use a forward declaration of B in Ah instead of including Bh The downside to this is that all functions that need to make use of B must then go in A.cc (which will definitely have to include Bh). 但是,正如Logicrat的答案所指出的,您可以选择在A中仅​​包含指向B的指针。在这种情况下,您可以在Ah中使用B的前向声明,而不包括Bh。这样做的缺点是,所有需要执行然后必须使用B来输入A.cc(肯定必须包含Bh)。 The advantage is that you can greatly reduce the quantity of code in the header files (since each #include copies an entire header file into the current file just before compilation), which can speed up compilation times. 这样做的好处是可以大大减少头文件中的代码量(因为每个#include都在编译前将整个头文件复制到当前文件中),这可以缩短编译时间。

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

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