将C代码包装到C ++对象中 [英] Wrapping C code into a C++ object

查看:59
本文介绍了将C代码包装到C ++对象中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最好的方法是什么?

我应该制作一个纯粹的虚拟课程然后

来自那个吗?我希望基类具有定义的

函数,这样我就不必在派生类中重新执行

工作了。这是

不是用于什么对象?

请说明如何正确地做到这一点。

我不想要代码,只是指示如何使用
完成工作。

任何评论或链接都会有所帮助。


C代码是OpenGL 。我只想要一个类

,以便将来的项目更轻松。

如果需要,我可以发送源代码完成

到目前为止。我不相信浪费空间,所以

不在这篇文章中。


提前致谢,

Tom the Cunuck。

解决方案

2005年12月3日星期六11:04:11 -0500,Tom the Canuck

< tv *** @ sympatico.ca>写道:

最好的方法是什么?
我应该制作一个纯粹的虚拟课程,然后从中衍生出来吗?我希望基类定义了
函数,这样我就不必在派生类中重新完成
工作了。这不是什么对象的用途吗?


''实施继承''不一定是继续进行
的最佳方式。 C框架通常是基于对象的,但不是多态的。所以

''委托''可能比''继承''更好。

请说明如何正确地做到这一点。
我不是想要代码,只是指示如何正确地完成工作。
任何评论或链接都会有所帮助。

C代码是OpenGL。我只想要一个类来使未来项目的生活更轻松。
如果需要,我可以发送源代码到目前为止完成
。我不相信浪费空间,所以
它不在这篇文章中。




我对OpenGL一无所知,但使用'OpenGL C ++进行谷歌搜索''

产生许多有趣的链接。也许有人已经为你完成了工作



祝福,

Roland Pibinger




Tom the Canuck写道:

最好的方法是什么?
我应该制作一个纯粹的虚拟课程吗?那么是从那衍生出来的?我希望基类定义了
函数,这样我就不必在派生类中重新完成
工作了。这不是什么对象的用途吗?


哇!为什么你认为你需要继承?


如果你想让多个对象共享一个纯粹的虚拟类,那么继续使用它是非常有用的



常用界面并且是多态的。如果你不是b $ b,不要这样做。


如果你想在

之间分享一些代码从接口派生的不同类,然后

一个选项是将代码放在另一个类

并使用多重继承。来自

的界面(纯虚拟类)和

实现类。


但是,如果你只是想要C代码包裹起来

以方便使用 - 不要使用继承,有

没有意义。只需上课。


如果你想在C ++中使用OO,那么一个好的

起点是Bjarne Stroustrup的主页。

他对于

主题表达他的观点并不害羞。 < wink>

请说明如何正确地做到这一点。
我不想要代码,只是指示如何正确地完成工作。
任何评论或链接都会有所帮助。

C代码是OpenGL。我只想要一个类来使未来项目的生活更轻松。
如果需要,我可以发送源代码到目前为止完成
。我不相信浪费空间,所以
不在这篇文章中。

提前致谢,汤姆the Cunuck。




HTH


James M


Tom the Canuck写道:

最好的方法是什么?


没有最好的编程方式。

我应该制作一个纯虚拟课程,然后从中衍生出来吗?我希望基类定义了
函数,这样我就不必在派生类中重新完成
工作了。这不是什么对象的用途吗?


这是一种定义对象的单一方式。用于。

http:// en .wikipedia.org / wiki / Object _... ter_science%29

请说明如何正确执行此操作。
我不想要代码,只需一个指针如何正确地完成工作。
任何评论或链接都会有所帮助。

C代码是OpenGL。我只想要一个类来使未来项目的生活更轻松。
如果需要,我可以发送源代码到目前为止完成
。我不相信浪费空间,所以
它不在这篇文章中。




一个简单的方法就是将C代码包装成一个简单的类。


A级

{

公开:

void f()

{

c_f(& acs); //从f()调用C函数

}


私有:

a_C_struct acs;

};


如果您认为实施可能会改变(例如通过更改库
),您可以隐藏实现(在这种情况下为openGL) )

使用所谓的pimpl成语(google for it)。

Jonathan


What would be the best way to proceed?
Should I make a pure virtual class and then
derive from that? I want the base class to have
functions defined so that I don''t have to do the
work all over again in the derived class. Is this
not what objects are for?
Please illuminate on how to do this properly.
I don''t want code, just a pointer on how to
do the job right.
Any comments or links will help.

The C code is OpenGL. I just want a class
to make life easier for future projects.
If requested, I can send the source code done
up to now. I do not believe in wasting space, so
it is not in this post.

Thanks in advance,
Tom the Cunuck.

解决方案

On Sat, 3 Dec 2005 11:04:11 -0500, "Tom the Canuck"
<tv***@sympatico.ca> wrote:

What would be the best way to proceed?
Should I make a pure virtual class and then
derive from that? I want the base class to have
functions defined so that I don''t have to do the
work all over again in the derived class. Is this
not what objects are for?
''Implementation inheritance'' isn''t necessarily the best way to
proceed. C frameworks are often ''object based'' but not polymorphic. So
''delegation'' may be better than ''inheritance''.
Please illuminate on how to do this properly.
I don''t want code, just a pointer on how to
do the job right.
Any comments or links will help.

The C code is OpenGL. I just want a class
to make life easier for future projects.
If requested, I can send the source code done
up to now. I do not believe in wasting space, so
it is not in this post.



I know nothing about OpenGL but a Google search with ''OpenGL C++''
yields many interesting links. Maybe someone has already done the work
for you.

Best wishes,
Roland Pibinger



Tom the Canuck wrote:

What would be the best way to proceed?
Should I make a pure virtual class and then
derive from that? I want the base class to have
functions defined so that I don''t have to do the
work all over again in the derived class. Is this
not what objects are for?
Woah! Why do you reckon you need inheritance here?

Inheriting from a pure virtual class is incredibly
useful if you want multiple objects to share a
common interface and be polymorphic. If you
don''t, don''t do that.

If you want to share some code between
different classes derived from an interface, then
one option is to put the code in another class
and use multiple inheritance. Derive from
both the interface (pure virtual class) and the
implementation class.

However, if you just want the C code wrapped up
for convenient use -- don''t use inheritance, there''s
no point. Just make the class.

If you want advice on OO in C++ then a good
place to start is Bjarne Stroustrup''s homepage.
He is not shy about expressing his views on the
topic. <wink>
Please illuminate on how to do this properly.
I don''t want code, just a pointer on how to
do the job right.
Any comments or links will help.

The C code is OpenGL. I just want a class
to make life easier for future projects.
If requested, I can send the source code done
up to now. I do not believe in wasting space, so
it is not in this post.

Thanks in advance,
Tom the Cunuck.



HTH

James M


Tom the Canuck wrote:

What would be the best way to proceed?
There is no "best" way in programming.
Should I make a pure virtual class and then
derive from that? I want the base class to have
functions defined so that I don''t have to do the
work all over again in the derived class. Is this
not what objects are for?
Well that is a singular way to define what "objects" are for.

http://en.wikipedia.org/wiki/Object_...ter_science%29
Please illuminate on how to do this properly.
I don''t want code, just a pointer on how to
do the job right.
Any comments or links will help.

The C code is OpenGL. I just want a class
to make life easier for future projects.
If requested, I can send the source code done
up to now. I do not believe in wasting space, so
it is not in this post.



One easy way would be to wrap the C code into a simple class.

class A
{
public:
void f()
{
c_f(&acs); // call the C function from f()
}

private:
a_C_struct acs;
};

If you think the implementation could change (by changing the library
for example), you could hide the implementation (openGL in this case)
using the so called "pimpl" idiom (google for it).
Jonathan


这篇关于将C代码包装到C ++对象中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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