小编Bar*_*ick的帖子

C 代码中 C++ 代码中的 sizeof 类作为编译时常量

我需要 C 代码中的编译时间常数:

#define CLASSFROMCXX_SIZE /* compile time constant /*
Run Code Online (Sandbox Code Playgroud)

在 C++ 代码中我可以这样写:

#define CLASSFROMCXX_SIZE sizeof(ClassFromCXX)
Run Code Online (Sandbox Code Playgroud)

但我不能在 C 代码中使用 ClassFromCXX 。我可以编写一个返回 sizeof(ClassFromCXX) 的函数,但我需要编译时常量,而不是某些运行时值。

是否可以在不生成代码的情况下实现?我可以使用哪些现有的代码生成工具来实现这一目标?

这更多的是一个理论问题,但作为一个例子:我在 C 代码中使用了一些用 C++ 编写的库,所以我用 C 编写了包装器。我必须以某种方式存储 C++ 中的类。相关问题是:How to call a c++ class and its method from ac file 但它使用堆分配/堆栈分配。

c c++

5
推荐指数
1
解决办法
309
查看次数

标签 统计

c ×1

c++ ×1