sem_t 联合/结构 C++继承
我正在 Linux (RHEL 5.3) 上移植一些旧的 C++ 项目。
情况如下
#include <semaphore.h>
class OldClass: public sem_t
这曾经有效,因为直到 glibc-2.3.3.20040420 sem_t 都是一个结构。 现在,新版本的 glib 不允许 union => 继承。所以编译不起作用。
情况如何:
typedef struct { struct
_pthread_fastlock __sem_lock;
int __sem_value;
_pthread_descr __sem_waiting; }
sem_t;
情况如何:
typedef union {
char __size[__SIZEOF_SEM_T];
long int __align; }
sem_t;
解决此问题的最佳方法是什么?如何“包装” sem_t 的功能?
非常感谢!
======稍后编辑======================================
OldClass“稍后”使用由其他一些类(项目相当大):因此,我正在寻找一种保持相同接口的方法,这样我就可以避免重新编写对 OldClass 的所有调用。
我在想是否有办法创建一个类 MySem_t 来包装 sem_t;OldClass 然后继承 MySem_t...这听起来可行吗?
谢谢。
I'm porting some old C++ project on Linux (RHEL 5.3).
The situation is the following
#include <semaphore.h>
class OldClass: public sem_t
This used to work because till glibc-2.3.3.20040420 sem_t was a struct.
Now, with the newer version of glib is a union =>inheritance not allowed. so the compilation doesn't work.
how it was:
typedef struct { struct
_pthread_fastlock __sem_lock;
int __sem_value;
_pthread_descr __sem_waiting; }
sem_t;
how it is:
typedef union {
char __size[__SIZEOF_SEM_T];
long int __align; }
sem_t;
What would be the best approach to fix this?How can I "wrap" the functionality of sem_t?
Many thanks!
======later edit====================================
OldClass is "later" used by some other class (project is quite big) : therefore, I'm looking for a way to keep the same interface, so I can avoid re-writing all the calls to OldClass.
I was thinking if there a way to create a class MySem_t that wraps around sem_t;OldClass would then inherit MySem_t... does this sound feasible?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您必须创建一个类型为 sem_t 的类成员。为了简化移植,您可以使用如下类型转换方法:
You have to create a class member with type sem_t. To simplify porting, you can use type cast methods like this:
从
sem_t
继承并不是有意为之,因此如果您要移植的代码选择继承而不是存储单独的信号量,那么它们显然会存在一些问题。我建议您不要尝试重复旧代码解决问题的方式,而是进行一些分析和重构,以便
OldClass
不会继承自sem_t
,而是 在必要时使用sem_t
。我对代码一无所知,因此无法进一步指导您,但这可能是您最好的攻击媒介。
Inheriting from
sem_t
was never intended, so the code you're porting clearly had some issues if they chose to inherit rather than storing a separate semaphore.Rather than trying to duplicate the way the old code approaches the problem, I would suggest you do some analysis and refactor such that
OldClass
doesn't inherit fromsem_t
, but uses asem_t
where necessary.I know nothing about the code so I can't direct you further, but that's probably your best attack vector.
这里继承的目的到底是什么?如果您只是追求方法语法的语法糖(想想 .Net 中的扩展方法),那么您可能应该只使用自由函数。
如果您确实需要单独的类,那么您应该倾向于组合而不是继承。即,
OldClass
应该拥有一个sem_t
成员,而不是成为一个sem_t
。What exactly is the purpose of inheritance here? If you're just after syntactic sugar of method syntax (think extension methods in .Net), then you should probably just use free functions.
If you actually need the separate class, then you should favor composition over inheritance. I.e.
OldClass
should have asem_t
member, not be asem_t
.获取 OldClass 的定义,并重写它,其中 OldClass 具有 sem_t。尽管您希望定义本身能够编译,但不要担心它是一个好的重写。现在,尝试重新编译。为一连串的错误消息做好准备,因为代码很可能使用 OldClass 作为简单的结构。浏览大量错误消息。
如果您幸运的话,不会有大量代码直接与 sem_t 成员混淆,因为您必须更改每一行。 (无论如何,即使没有不恰当的继承,您也必须更改它们,因为 sem_t 从根本上改变了。)不幸的是,认为从 sem_t 继承是个好主意的程序员可能不认为有必要将 sem_t 视为不透明数据类型,除某些功能外作为一个整体使用。
Take the definition OldClass, and rewrite it, with OldClass having a sem_t. Don't worry about it being a good rewrite, although you want the definition itself to compile. Now, try recompiling. Be prepared for cascades of error messages, because it's likely that the code was using OldClass as a simple struct. Go through the mass of error messages.
If you're lucky, not a whole lot of code messed with the sem_t members directly, because you will have to change each one of those lines. (You'd have had to change them anyway, even without the infelicitous inheritance, since sem_t changed fundamentally.) Unfortunately, programmers who thought it a good idea to inherit from sem_t likely didn't think it necessary to treat sem_t as an opaque data type, to be used as a whole except for certain functions.