当前cpp使用函数只需要满足在当前cpp使用其他cpp内函数的要求即可,不需要再考虑函数中使用的变量是否在cpp中定义
示例如下:
在test.h中定义的变量 value ;
在test.cpp中的函数func()和foo()使用变量 value;
在main.cpp中调用func()和foo()函数。//我们在调用func()和foo()的时候,并不需要考虑value是在哪定义的,只需要声明一下函数即可。至于func()和foo()中是如何调用value(通过包含头文件test.h获得)我们不用去考虑,也就说每层使用只要保证上一层应用符合条件即可。
//test.h中定义变量
#ifndef TEST_H
#define TEST_H
int value;
#endif // TEST_H
//test.cpp中定义函数
#include "test.h"
#include <QDebug>
void func(){
value = 100;
}
void foo()
{
qDebug()<<value;
}
//main.cpp中调用函数
#include <QCoreApplication>
#include <QDebug>
extern void func();
extern void foo();
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
func();
foo();
return a.exec();
}