具有 C 链接的函数能够返回类类型? [英] Functions with C linkage able to return class type?

查看:22
本文介绍了具有 C 链接的函数能够返回类类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在具有 C 链接的 dll 中观察到一个函数.此函数返回类类型.我不确定这是如何实现的,因为 C 不理解类.
我自己编写了一个示例 dll 并编写了程序,并注意到 VC++ 编译器显示了对此效果的警告,但并没有阻止您.程序能够获取该函数的 GetProcAddress 并调用它来接收返回的对象.类定义可供程序使用.
此外,如果我编写一个带有 C 链接的函数,该函数返回一个甚至没有导出此类的类类型,编译器不会发出任何警告.只要类定义可供程序使用,程序就可以从 dll 使用此函数.
关于这是如何工作的任何想法?这种行为是否特定于编译器/平台?

I observed a function in a dll which has C linkage. This function returns class type. I am not sure how this is made possible as C doesn't understand class.
I wrote a sample dll and program myself and noted that the VC++ compiler shows a warning to this effect but doesn't stop you. The program is able to GetProcAddress of this function and call it to receive the returned object. The class definition was made available to program.
Furthermore, if I write a function with C linkage that returns a class type where this class is not even exported, the compiler doesn't issue any warning. The program can consume this function from the dll provided the class definition is made available to it.
Any thoughts on how this works? Is such a behavior compiler/platform specific?

推荐答案

您误解了 extern "C" 的行为.

它只会通过阻止 名称修改.它不会使函数更多 C"或更少 C++".extern "C" 添加到 C++ 函数的唯一附加限制是不得重载.

It only impacts the name of the function in the object file by preventing name mangling. It does not make a function neither "more C" or "less C++". The only additional limitation which extern "C" adds to a C++ function is that is shall not be overloaded.

这篇关于具有 C 链接的函数能够返回类类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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