C++ by-reference argument and C linkage

前端 未结 5 2095
执笔经年
执笔经年 2021-01-08 00:07

I have encountered a working (with XLC8 and MSFT9 compilers) piece of code, containing a C++ file with a function defined with C linkage and a reference argument. This bugs

5条回答
  •  悲哀的现实
    2021-01-08 00:44

    A reference is an alternate name for an object. Technically, there is nothing in C that could map directly to a C++ reference.

    The obvious implementation of a reference is as a (constant) pointer that is dereferenced each time it is used. So, depending on how your compiler implements references, your code might work. But it is not correct.

    The solution is to write a C function that receives either a real object or a pointer to that object and call the C++ function from this.

提交回复
热议问题