将头文件中的特定函数包含在c ++中的代码中 [英] Include specific function from a header file into the code in c++

查看:158
本文介绍了将头文件中的特定函数包含在c ++中的代码中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在python中,可以从不同的模块导入特定的功能集,而不是导入整个文件

In python, one can import specific feature-sets from different modules, rather than importing the whole file

ex:

而不是使用导入数学并使用 print math.sqrt(4),导入该函数直接:

Instead of using import math and using print math.sqrt(4), importing the function directly:

from math import sqrt
print sqrt(4)

它运作正常。

And it works just fine.

C C ++ 中,必须包含整个头文件才能够只使用它提供的一个功能。例如,在C ++中

Where as in C and C++, one has to include the whole header file to be able to use just one function that it provides. Such as, in C++

#include<iostream>
#include<cmath>
int main(){
    cout<<sqrt(4);
    return 0;
}

C 代码将也是类似的(不一样)。

C code will also be similar (not same).

是否有可能就像在python的情况下一样,可以只包含头文件中的一个函数到他们的程序中?

ex:包括 cmath中的 sqrt()函数/ code>?


可以吗?

Is it possible that just like as it was in case of python, one can include just one function from a header file into their program?
ex: including just the sqrt() function from cmath?

Could it be done?

推荐答案

不,这是不可能的。 C ++缺少一个真正的模块系统,所以我们留下了预处理器包含。添加新类型模块系统的提议没有进入C ++ 11。请参阅 C ++模块 - 为什么要从C ++ 0x中删除它们?他们会在稍后回来吗?以获取有关该提案的更多信息。

No, it is not possible. C++ lacks a true module system, so we are left with preprocessor includes. A proposal to add a new kind of module system did not make it into C++11. See C++ Modules - why were they removed from C++0x? Will they be back later on? for more information on that proposal.

如果这是关于您自己的库,您唯一的机会是将库分成较小的独立图书馆。如果图书馆不是你的和/或你不能改变它,你将不得不忍受它。但无论如何,真正的问题是什么?

If this is about your own library, your only chance is to split the library into smaller, independent libraries. If the library is not yours and/or you cannot change it, you'll have to live with it. But what's the real problem, anyway?

这篇关于将头文件中的特定函数包含在c ++中的代码中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆