C ++真的可移植吗? [英] Is C++ really portable?

查看:78
本文介绍了C ++真的可移植吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



让我们说我们要制作一个系统库,并且有两个先决条件的课程。


*它将被用于_a_lot_,因此需要非常高效。

*它可以有两种不同的实现。 (例如Unix / Windows)


我感到困惑。到目前为止,我见过的唯一解决方案是使用

设计模式''抽象工厂'',它给了我一个纯粹的
虚拟接口的指针(可以有任何实施)。但是那个

强迫我每次需要一个实例时就进行内存分配

该类!这几乎是有效的。


我必须忍受这个吗?或者我需要制作某种类型的


类SystemThing

{

....

private:


strange_unix_var uv;

strange_win_var wv;

}


然后在库实现中使用适当的变量?


这个问题通常如何解决?


谢谢

Daniel Marcus


Let''s say we''re gonna make a system library and have a class with
two prerequisites.

* It will be used _a_lot_, and therefore need to be really efficient.
* It can have two different implementations. (e.g. Unix/Windows)

I feel stuck. The only solution I''ve seen so far is using the
design pattern ''abstract factory'' that gives me a pointer to a pure
virtual interface (which can have whatever implementation). But that
forces me to make a memory allocation every time I need an instance
of that class! That''s all but efficient.

Do I have to live with this? Or do I have to make some kind of

class SystemThing
{
....
private:

strange_unix_var uv;
strange_win_var wv;
}

and then use the appropriate variable in the library implementation?

How is this problem commonly solved?

Thanks
Daniel Marcus


推荐答案

DeMarcus写道:
DeMarcus wrote:

假设我们要制作一个系统库,并且有一个具有两个先决条件的课程。

*它将被用于_a_lot_,因此需要非常高效。
*它可以有两种不同的实现方式。 (例如Unix / Windows)

我感到困惑。到目前为止,我见过的唯一解决方案是使用
设计模式抽象工厂,它给我一个指向纯粹虚拟接口的指针(可以有任何实现)。但是,每当我需要该类的实例时,我就会强迫我进行内存分配!这几乎是有效的。

Let''s say we''re gonna make a system library and have a class with
two prerequisites.

* It will be used _a_lot_, and therefore need to be really efficient.
* It can have two different implementations. (e.g. Unix/Windows)

I feel stuck. The only solution I''ve seen so far is using the
design pattern ''abstract factory'' that gives me a pointer to a pure
virtual interface (which can have whatever implementation). But that
forces me to make a memory allocation every time I need an instance
of that class! That''s all but efficient.




这取决于你需要它的效率。每个std :: string将

也需要分配其内存,许多程序也使用_a_lot_

字符串。

无论如何,我不明白你为什么需要这里的工厂。使用工厂

来决定在运行时实例化哪个类。但是你的程序不可能在运行时从unix系统迁移到windows

系统,不是吗?那你为什么不用#ifdef来

根据你的程序系统选择代码的各个部分

编译为什么?



That depends on how efficient you need it to be. Each std::string will
also need to allocate its memory, and many programs also use "_a_lot_"
of strings.
Anyway, I don''t see why you would need a factory here. A factory is used
to decide which class to instantiate at runtime. But an instance of
your program is unlikely to migrate from a unix system to a windows
system while running, isn''t it? So why don''t you just use #ifdef to
select the parts of the code based on the system your program is
compiled for?





Rolf Magnus写道:


Rolf Magnus wrote:
DeMarcus写道:

DeMarcus wrote:

让我们说我们要制作一个系统库,并有一个具有两个先决条件的课程。

*将使用_a_lot_ ,因此需要非常高效。
*它可以有两种不同的实现方式。 (例如Unix / Windows)

我感到困惑。到目前为止,我见过的唯一解决方案是使用
设计模式抽象工厂,它给我一个指向纯粹虚拟接口的指针(可以有任何实现)。但是,每当我需要该类的实例时,我就会强迫我进行内存分配!这几乎是有效的。
Let''s say we''re gonna make a system library and have a class with
two prerequisites.

* It will be used _a_lot_, and therefore need to be really efficient.
* It can have two different implementations. (e.g. Unix/Windows)

I feel stuck. The only solution I''ve seen so far is using the
design pattern ''abstract factory'' that gives me a pointer to a pure
virtual interface (which can have whatever implementation). But that
forces me to make a memory allocation every time I need an instance
of that class! That''s all but efficient.



这取决于你需要它的效率。每个std :: string也需要分配它的内存,许多程序也使用_a_lot_
字符串。
无论如何,我不明白为什么你需要一个工厂在这里。使用工厂来决定在运行时实例化哪个类。但是你的程序实例在运行时不太可能从unix系统迁移到windows
系统,不是吗?那你为什么不用#ifdef来根据你的程序编译的系统来选择代码部分?


That depends on how efficient you need it to be. Each std::string will
also need to allocate its memory, and many programs also use "_a_lot_"
of strings.
Anyway, I don''t see why you would need a factory here. A factory is used
to decide which class to instantiate at runtime. But an instance of
your program is unlikely to migrate from a unix system to a windows
system while running, isn''t it? So why don''t you just use #ifdef to
select the parts of the code based on the system your program is
compiled for?




我试图远离非C ++的东西,但为了让它多一点

清楚我在一个相同的应用程序可以使用两个的位置

不同类型的线程库(pthread和sproc),它们不兼容
。这意味着我真的希望能够包装并且

提取该库,这样我就可以动态加载它而无需

重新编译应用程序。


我被困在指针上,不是吗?



I tried to keep away from non-C++ things, but to make it a little more
clear I''m in a position where a very same application can use two
different types of thread libraries (pthread and sproc) which are not
compatible. This means I would really like to be able to wrap and
extract that library so I can load it dynamically without the need to
recompile the application.

I''m stuck to pointers, ain''t I?


" DeMarcus" <无**** @ tellus.orb>在留言中写道

新闻:40 *********************** @ news3.bahnhof.se ...
"DeMarcus" <no****@tellus.orb> wrote in message
news:40***********************@news3.bahnhof.se...
我试图远离非C ++的东西,但为了使它更加清晰我明白我在一个同样的应用程序可以使用两个的位置
不兼容的不同类型的线程库(pthread和sproc)。这意味着我真的希望能够包装并提取该库,以便我可以动态加载它而无需重新编译应用程序。
I tried to keep away from non-C++ things, but to make it a little more
clear I''m in a position where a very same application can use two
different types of thread libraries (pthread and sproc) which are not
compatible. This means I would really like to be able to wrap and
extract that library so I can load it dynamically without the need to
recompile the application.




我真的不明白为什么你想能够使用两个不同的
库,除非你在谈论两个不同的libs用于两个不同的
操作系统''秒。在后一种情况下,你不需要任何指针,正如Rolf Magnus

已经指出的那样。也许有助于创建一个通用界面,

将调用转发到正确的库。 #defines将控制函数转发调用的哪个

库。这样你就可以隐藏实际用户代码中的所有

#define杂技 - 不是必要的,但也许它

看起来更好:)


hth

-

jb


(如果您想通过电子邮件回复,请用y替换y )



I do not really understand why you want to be able to use two different
libraries, unless you are talking about two different libs for two different
OS''s. In the latter case, you do not need any pointers, as Rolf Magnus
already pointed out. Maybe it helps to create a common interface that
forwards the calls to the right library. #defines will control to which
library the functions will forward the calls. This way you hide all the
#define acrobatics from the actual user code -- not neccessary, but maybe it
looks nicer :)

hth
--
jb

(replace y with x if you want to reply by e-mail)


这篇关于C ++真的可移植吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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