C风格的单位 - > C ++类,实现函数/结构问题 [英] C-style unit -> C++ class, implementation function / structure issue

查看:79
本文介绍了C风格的单位 - > C ++类,实现函数/结构问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将C风格的单元转换为C ++类。我有一个

实现函数,该函数在.cpp文件中定义(因此从.h文件中存在的接口隐藏了

)。它使用了只需要实现所需的

结构,因此它也是.cpp文件中声明的



现在,当将它转换为类时,类定义存在于.h文件中,因为它是接口所需要的。实施

函数属于私有功能。访问说明符,因此将它们从.cpp文件移动到.h文件中。
。但是,它需要访问上面提到的

结构。看来我必须在.h文件中包含结构,使其可以公开访问!


由于没有公共成员函数使用该结构,我知道

没人能用它做什么,所以一切都好。但它只是

看起来很难公开访问它应该是隐藏在实现中的




有没有实现这个目标的方法?


Jason

I am converting a C-style unit into a C++ class. I have an
implementation function that was defined in the .cpp file (so it was
hidden from the interface that exists in the .h file). It uses a
structure that is only needed by the implementation, so it were
declared in the .cpp file, as well.

Now, when converting this into a class, the class definition exists in
the .h file, since it''s required by the interface. The implementation
functions go under a "private" access specifier, so they are moved
from the .cpp file into the .h file. But, it needs access to the
above mentioned structure. It seems I must include the structure in
the .h file, making it publicly accessible!

Since no public member functions make use of the structure, I know
that no one can do anything with it, so everything is ok. But it just
seems UGLY to make something publicly accessible when it should be
hidden in the implementation.

Is there a way to achieve this?

Jason

推荐答案

参见PIMPL习语。通常它涉及动态分配。


我在这里找到了相当不错的概述:
http://www.gamedev.net/reference/art...rticle1794.asp


我发现它需要动态分配,并且对于任何这些功能都要额外取消引用

。由于我正在开发一个性能

的应用程序,这是不可接受的。

See PIMPL idiom. Generally it involves dynamic allocation.

I found a pretty decent rundown of it here:
http://www.gamedev.net/reference/art...rticle1794.asp

I see that it requires dynamic allocation, and an extra dereference
for any of these functions. Since I am developing a performance
application, this is unacceptable.


对于危险

避免动态分配PIMPL,请参阅相关的GOTW。
For hazards
of avoiding dynamic allocation for PIMPL, see the relevant GOTW.



你的意思是 http://www.gotw.ca/gotw/028.htm

我想,真正的问题是:是否有比我更好的方式?$ $
我已经在做什么?我不想以任何

成本获得最终结果。如果这样做比复制它更复杂,如果剩下的那样 -

,那么我会保持原样。这只是C ++中的一个简单的烦恼,

在许多接口/实现< sigh>中,但是如果C ++想要它,那么
就是这样,那么我猜它应该是这样的。我不会尝试和

争取*任何可能的方法*来获得我的实现结构

在.cpp中定义,如果它需要很大(甚至是中等)

复杂性。


我应该更准确地改写我的问题。


感谢您的回复,

Jason

You mean http://www.gotw.ca/gotw/028.htm ?
I guess, the real question is: Is there a more proper way than what I
am already doing? I don''t want to achieving the end result at any
cost. If it''s more complicated to do this than it would be if left as-
is, then I''ll leave it as-is. It''s just a simple annoyance in C++,
among many with interfaces/implementations <sigh>, but if C++ wants it
to be this way, then I guess it should be this way. I won''t try and
fight for *any method possible* to get my implementation structures
defined in the .cpp, if it requires great (or even moderate)
complexity.

I should have rephrased my question more accurately.

Thanks for your reply,
Jason


我不确定我理解。你是如何在没有
的情况下得到的?
I''m not sure I understand. How did you get by in C without

定义标题中的结构?你可以在一个类中完全包装

相同的技术。
defining the struct in the header? You can wrap exactly the
same technique in a class.



(我使用的是C ++编译器,所以我的原始代码在技术上并不是b $ b普通的C代码......我正在使用没有类的C ++。我使用的是C风格的
单位函数,其中的实现存在于.cpp

文件中,并且界面在.h中可见文件)


我在.cpp文件中定义了结构,因为只有.cpp文件中的实现

函数才使用它。

任何接口函数都不需要该结构,因此它不必存在于.h文件中。


我的问题是我当我将这个''unit''转换成C ++类时,我不想将结构移动到.h文件

。但是,我现在意识到我

可以将结构放在.h文件中,在类的私有部分下,

并且它将被隐藏起来接口。一切都很好。


谢谢,

Jason

(I was using a C++ compiler, so my original code wasn''t technically
plain C code... I was using C++ without classes. I was using C-style
units of functions, where the implementation existed all in the .cpp
file, and the interface was visible in the .h file)

I defined the struct in the .cpp file, since only the implementation
functions in the .cpp file used it. The struct was not required for
any interface functions, so it didn''t have to exist in the .h file.

My issue is that I didn''t want to move the struct into the .h file
when I converted this ''unit'' into a C++ class. But, I now realize I
can put the struct in the .h file, under the class''s private section,
and it''ll be hidden from the interface. All is well.

Thanks,

Jason


7月12日晚上7:15,Jason Doucette< jdouce ... @ gmail.comwrote:
On Jul 12, 7:15 pm, Jason Doucette <jdouce...@gmail.comwrote:

I'm不确定我理解。你是如何在没有

定义头部结构的情况下在C中获得的?你可以在一个类中完全包装

相同的技术。
I''m not sure I understand. How did you get by in C without
defining the struct in the header? You can wrap exactly the
same technique in a class.


(我使用的是C ++编译器,所以我的原始代码在技术上并不是
plain C代码......我在没有类的情况下使用C ++。我使用的是C风格的

单位的函数,其中的实现存在于.cpp

文件中,并且界面在.h文件中可见)
(I was using a C++ compiler, so my original code wasn''t technically
plain C code... I was using C++ without classes. I was using C-style
units of functions, where the implementation existed all in the .cpp
file, and the interface was visible in the .h file)


我在.cpp文件中定义了结构,因为只有实现
$ b .cpp文件中的$ b函数使用它。

任何接口函数都不需要该结构,因此它不必存在于.h文件中。
I defined the struct in the .cpp file, since only the implementation
functions in the .cpp file used it. The struct was not required for
any interface functions, so it didn''t have to exist in the .h file.



换句话说,编译防火墙(pimpl)成语。你可以用$ c $ b在C ++中完全相同。

In other words, the compilation firewall (pimpl) idiom. You can
do exactly the same in C++.


我的问题是我不想将结构移动到。 h文件

当我将这个''unit''转换成C ++类时。但是,我现在意识到我

可以将结构放在.h文件中,在类的私有部分下,

并且它将被隐藏起来接口。一切都很好。
My issue is that I didn''t want to move the struct into the .h file
when I converted this ''unit'' into a C++ class. But, I now realize I
can put the struct in the .h file, under the class''s private section,
and it''ll be hidden from the interface. All is well.



是的。 C ++为您提供了选择; C没有。


-

James Kanze(Gabi Software)电子邮件: ja ********* @ gmail.com

Conseils eninformatiqueorientéeobjet/

Beratung in objektorientierter Datenverarbeitung

9placeSémard,78210 St.-Cyr-l''coco,France,+ 33(0)1 30 23 00 34

Yes. C++ gives you the choice; C didn''t.

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l''école, France, +33 (0)1 30 23 00 34


这篇关于C风格的单位 - &gt; C ++类,实现函数/结构问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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