伙计们,我有一个问题,我不知道如何在一个 header 中连接多个 header ,我们称它为“主 header ”并使用该 header 中的函数,例如
// A.h
#include <iostream>
class A {
public:
A();
void showMessage(string msg);
};
// B.h
class B {
public:
B();
void CountMessages()
};
// C.h
#include "A.h"
#include "B.h"
class C : public B, public A {
public:
C();
void DoSomething();
}
并且我想使用类“c”的对象和/或类“a”从类“b”调用函数 CountMessages,例如
//c.cpp
#include "C.h"
#include "B.h"
#include "A.h"
extern A b_obj;
extern B a_obj;
C::DoSomething() {
b_obj.CountMessages();
a_obj.showMessage("Hello");
}
最佳答案
I have a question, I don't know how can I connect many headers in one header, let's call it "Master header" and use functions from that headers,
创建文件并为所有其他头文件添加 #include
行。
MasterHeader.h:
#include "A.h"
#include "B.h"
#include "C.h"
c.cpp:
#include "MasterHeader.h"
警告:我一般不推荐这种做法。这仅在 "MasterHeader.h"
提供的接口(interface)是主要接口(interface)时才有用,"A.h"
、"B.h"
和 “C.h”
服务于易于维护的目的。
关于C++ 将多个 header 连接到一个 header 并使用这些 header 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28013504/