简体   繁体   English

将C结构作为C ++类访问

[英]Accessing C struct as a C++ class

Is there a workaround for this bug in gcc ? 这个bug在gcc中有解决方法吗?

Specifically, I think I am running into the bug when compiling a wrapper for pthread_mutex_t. 具体来说,我认为在为pthread_mutex_t编译包装器时遇到了这个问题。 The header looks like: 标题看起来像:

class DerivedClass: public pthread_mutex_t{
  public:
     DerivedClass() {}
     ~DerivedClass(){}
     DerivedClass someFunction(){}
};

The code is from a legacy system and used to compile on GCC 3.2.x but won't do on GCC 4.1.2. 代码来自遗留系统,用于在GCC 3.2.x上编译,但不适用于GCC 4.1.2。

... In theory I guess I could recompile everything on a back-version GCC or refactor the referencing sources to disuse the wrapper, but I want to see if there is an easier way first. ...理论上我想我可以在后台版本的GCC上重新编译所有内容,或者重构引用源以废弃包装器,但我想先看看是否有更简单的方法。

Many Thanks. 非常感谢。

The bug is fixed in GCC 4.6.0, but if you need it now, use composition instead: 该错误已在GCC 4.6.0中修复,但如果您现在需要,请使用组合:

class DerivedClass {
  public:
    pthread_mutex_t mutex;
// ...
}

If what you need is a class which can be casted to a pthread_mutex_t * , then make sure your class has no virtual functions, and put the mutex as the first element in the class; 如果你需要的是一个可以转换为pthread_mutex_t *的类,那么确保你的类没有虚函数,并将互斥量作为类中的第一个元素; you can then convert between DerivedClass * and pthread_mutex_t * safely. 然后,您可以安全地在DerivedClass *pthread_mutex_t *之间进行转换。 Or add an operator pthread_mutex_t *() . 或者添加operator pthread_mutex_t *()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM