本文介绍了在启动时执行功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! Hello NG, 如果加载了一个特定的动态库,我需要确保该库中的一个 函数在启动时被执行(和我希望机制 只使用标准C ++)。这就是我所拥有的: void func() { //这是函数必须执行 } struct foo { foo(){func( );} } global_var; 我想就更优雅的替代方案征求意见。 (我 意识到优雅是主观的!) 谢谢! DaveHello NG,If a particular dynamic library gets loaded, I need to ensure that afunction in that library gets executed on startup (and I want the mechanismto use only Standard C++). Here''s what I''ve got:void func(){// This is the function that must be executed}struct foo{foo() {func();}} global_var;I would like to solicit suggestions on more elegant alternatives. (Irealize that elegance is subjective!)Thank you!Dave推荐答案 更多_sure_替代方案来调用该函数_upon_loading_ 动态库将来自动态库_loading_ 例程,这是特定于操作系统的。 至于优雅,你的方法很好。当然,如果你可以让你的 函数返回一个int,那么你可以让它变得更简单: int func(){/ * blah * / return 0; } int global_var = func(); VThe more _sure_ alternative to call that function _upon_loading_ of thatdynamic library would be to call it from the dynamic library _loading_routine, which is OS-specific.As for elegance, your method is fine. Of course, if you could make yourfunction to return an int, then you could make it a bit simpler:int func() { /* blah */ return 0; }int global_var = func();V 比这更糟糕(主观)。作为标准C ++语言(这里讨论的 )没有什么可说的动态加载库(还), 这不是问这个问题的最佳位置。或者冒着政治上不正确的风险,我可能会说,这不是问这个问题的地方。 ;-)如果你需要另一种方式来做你所要求的,你将需要在新闻组或其他专门用于你的b $ b平台/编译器的论坛中询问它。最有可能有办法做到这一点。 - WW aka Attila ::: 科尔的公理:这个星球的智慧总和是不变的。 人口正在增长。It is worse than that (being subjective). As the standard C++ language (theone discussed here) has nothing to say about dynamic load libraries (yet),this is not the best place for asking this question. Or running the risk ofbeing politically incorrect I may say, this is not the place to ask thisquestion. ;-) If you need another way to do what you have asked, you willneed to ask it in a newsgroup or other forum dedicated to yourplatform/compiler. Most probably there is a way to do that.--WW aka Attila:::Cole''s Axiom: The sum of the intelligence on the planet is a constant. Thepopulation is growing. (这里讨论的那个)对动态加载库没什么好说的,这不是问这个问题的最佳地方。或者冒着政治上不正确的风险我可能会说,这不是问这个问题的地方。 ;-)如果你需要另一种方式来做你所要求的,你将需要在新闻组或专门用于你的平台/编译器的论坛中提问。很可能有办法做到这一点。 - WW aka阿提拉 ::: 科尔的公理:智力的总和这个星球是一个常数。 人口正在增长。(the one discussed here) has nothing to say about dynamic load libraries (yet), this is not the best place for asking this question. Or running the riskof being politically incorrect I may say, this is not the place to ask this question. ;-) If you need another way to do what you have asked, you will need to ask it in a newsgroup or other forum dedicated to your platform/compiler. Most probably there is a way to do that. -- WW aka Attila ::: Cole''s Axiom: The sum of the intelligence on the planet is a constant. The population is growing. 我特别希望保持在标准C ++的范围内。我不希望 做任何特定于编译器的事情。我可能不应该提到 字词动态库。因为他们的存在自然会导致人们认为我正在寻找特定于平台的东西,即使我所说的 意图恰恰相反。所以,让我们用这种方式解决问题: 我需要确保在程序启动之前调用给定的函数,然后将控制转移到主要()。我想征求关于 的建议,我可以完全按照标准语言完成这个。 我正在寻找创造性/多样化的方式实现这一目标1) 可能改进我维护的代码; 2)扩展我的知识 C ++。 再次感谢, DaveI specifically want to stay within the realm of Standard C++. I don''t wantto do anything compiler-specific. I probably should not have mentioned thewords "dynamic library" since their presence naturally tends to cause peopleto think I''m looking for something platform-specific even though my statedintent was the opposite. So, let''s pose the problem this way:I need to ensure that a given function is called upon program startup beforecontrol is transferred to main(). I would like to solicit suggestions onways I may accomplish this that fall strictly within the standard language.I''m looking for creative / varied ways to accomplish this as a means of 1)Possibly improving the code I''m maintaining; 2) Expanding my knowledge ofC++.Thanks again,Dave 这篇关于在启动时执行功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-29 20:25