C和C ++编码标准 [英] C And C++ Coding Standards

查看:99
本文介绍了C和C ++编码标准的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是与问候的最佳实践,以C和C ++编码标准?开发者们应该被允许无可奈何地混合在一起。连接C时和C ++对象文件是否有任何并发​​症。

What are best practices with regards to C and C++ coding standards? Should developers be allowed to willy-nilly mix them together. Are there any complications when linking C and C++ object files.

想插槽库的东西,传统上是用C留在C和保存在单独的源文件?这是保持C $ C $℃的.c文件和c ++ code在.cpp文件。
当相克被解析后混合C和C ++ ++会有任何性能损失,因为类型安全检查不是在C做了什么?但在C ++中。将将链接C和C ++源$ C ​​$ C文件的最好方法。

Should things like socket libraries that traditionally is written in C remain in C and kept in seperate source files? That is keeping c code in .c files and c++ code in .cpp files. When mixing c and C++ after being parsed with g++ will there be any performance penalties, since typesafe checks are not done in C? but are in C++. Would would be the best way to link C and C++ source code files.

推荐答案

最大的问题是从调用C ++ code,反之亦然C函数。在这种情况下,你要确保你标记功能使用的externC有C链接。您可以直接使用头文件做到这一点:

The biggest issue is calling a C function from C++ code or vice versa. In that case, you want to make sure you mark the function as having "C" linkage using extern "C". You can do this in the header file directly using:

#if defined( __cplusplus )
extern "C" {
#endif

extern int myfunc( const char *param, int another_one );

#if defined( __cplusplus )
}
#endif

您需要的#如果是因为C code,它包括它不会理解的externC

You need the #ifs because C code that includes it won't understand extern "C".

如果您不想(或不能)改变头文件,你可以做到这一点在C ++ code:

If you don't want to (or can't) change the header file, you can do it in the C++ code:

extern "C" {
#include "myfuncheader.h"
}

您可以标记一个C ++函数具有C链接以同样的方式,然后你就可以从C code调用它。您不能重载函数或C ++类做到这一点。

You can mark a C++ function as having C linkage the same way, and then you can call it from C code. You can't do this for overloaded functions or C++ classes.

除此之外,应该没有问题的混合C和C ++。我们拥有一批几十年的老仍在使用我们的C ++ $ C C C的功能。

Other than that, there should be no problem mixing C and C++. We have a number of decades-old C functions that are still being used by our C++ code.

这篇关于C和C ++编码标准的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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