惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
Stack Overflow Blog
Stack Overflow Blog
Hugging Face - Blog
Hugging Face - Blog
博客园_首页
T
The Blog of Author Tim Ferriss
博客园 - 叶小钗
N
Netflix TechBlog - Medium
腾讯CDC
C
Check Point Blog
P
Proofpoint News Feed
Engineering at Meta
Engineering at Meta
GbyAI
GbyAI
S
SegmentFault 最新的问题
F
Fortinet All Blogs
美团技术团队
U
Unit 42
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 司徒正美
F
Full Disclosure
Recorded Future
Recorded Future
D
DataBreaches.Net
博客园 - 【当耐特】
Martin Fowler
Martin Fowler
J
Java Code Geeks
I
InfoQ
Y
Y Combinator Blog
A
About on SuperTechFans
AI
AI
爱范儿
爱范儿
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Forbes - Security
Forbes - Security
W
WeLiveSecurity
M
MIT News - Artificial intelligence
雷峰网
雷峰网
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Simon Willison's Weblog
Simon Willison's Weblog
Schneier on Security
Schneier on Security
The GitHub Blog
The GitHub Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
aimingoo的专栏
aimingoo的专栏
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
G
GRAHAM CLULEY
Know Your Adversary
Know Your Adversary
Latest news
Latest news
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
D
Docker
Recent Commits to openclaw:main
Recent Commits to openclaw:main
量子位
V2EX - 技术
V2EX - 技术
Project Zero
Project Zero

博客园 - 浪端之渡鸟

老鼠跑猫叫主人惊醒c++观察者模式实现 vs2010 mfc dll 调试编译的问题 std::stringstream 模版实现,类型转换,遇到空格不跳出 字节对齐,对opencv的影响 跨平台,c语言创建多级路径代码分享 吐槽VS2010,卡死后,一直不能点,强制关机后代码丢失 IOS App资源路径 opencv在MFC下使用的问题 fopen的打开方式 解决安装macports,不能更新的问题 坑爹啊,新手一个这问题折腾2天,xcode4.5拖上去控件真机调试就崩 静态页面转化 删除_svn文件夹 开发相关日志 WebClient.cs封装 symbian 输入控件不显示输入法解决: V3 V5宏定义 coco2d-x 重置z轴 htmlcontrol-for-symbian 源码解析
讨论exe获取dll提供的单例,并获取单例所提供的带有vector<class A>& STL容器的返回值的情况-提供1种解决方法
浪端之渡鸟 · 2013-04-11 · via 博客园 - 浪端之渡鸟

讨论exe获取dll提供的单例,并获取单例所提供的带有vector<class A>& STL容器的返回值的情况

开发过程中是苦逼的,遇到问题时,心情是烦躁滴,仔细想想问题是可以解决滴,而后是喜悦滴~废话略过...

一、问题描述:

1.首先看下单例的实现:

View Code

 1     template <class T>    
 2     class TSingleton    
 3     {    
 4     public:
 5         static  T* getInstance();
 6 
 7     private:
 8         TSingleton(void){}
 9         ~TSingleton(void){}
10         TSingleton(const TSingleton&){}
11         TSingleton & operator= (const TSingleton &){}
12 
13     private:
14         static std::auto_ptr<T> s_objInstance;
15     };
16 
17     template <class T> std::auto_ptr<T> TSingleton<T>::s_objInstance;
18 
19     template <class T>
20      T* TSingleton<T>::getInstance()
21      {
22         if (NULL != s_objInstance.get())
23             return s_objInstance.get();
24 
25         if(NULL == s_objInstance.get())    
26         {
27             s_objInstance.reset(new T);
28         }
29         return s_objInstance.get();
30     }

 对于单例的实现还是比较简单的,这里不讨论多线程加锁问题,只讨论dll中的单例,在exe中获取的情况,并能正常调用返回值类型为

vector<class A>& 等类型的接口,其他STL接口亦同。

我dll中的单例是这样定义的:

samples.h

View Code

 1 class  __declspec(dllexport) CSamples
 2 
 3 {
 4 
 5 public:
 6 
 7 vector<CSample*>& GetSamples()
 8 
 9 {
10 
11 return m_VecSamples;
12 
13 }
14 
15 private:
16 
17 vector<CSample*> m_VecSamples;//路径容器
18 
19 }
20 
21 typedef TSingleton<CSamples> SGCSamples;

 如果工程不分dll的话,我想不用描述该怎么调用了吧:

1 SGCSamples::getInstance()->GetSamples();

但是自从dll分离出来,在exe中再执行这个代码问题就来了,exe中调用

SGCSamples::getInstance(),会重新生成一份CSamples的static变量,也及是exe,dll中各一份,而且不一致,我测试了下,即使改成这样也不行:
Samples.h
...
__declspec(dllexport) typedef TSingleton<CSamples> SGCSamples;

exe 的.cpp

__declspec(dllimport)  typedef TSingleton<CSamples> SGCSamples;

为什么这样,暂时还没有理解,还请各位跟帖讨论

2.问题摆在这儿了,现在目标就是这么实现了,不废话,直接看解决方法:

二、提供接口返回实例的引用

//Samples.h
typedef    TSingleton<CSamples>    SGCSamples;
__declspec(dllexport) CSamples& GetSamples();


//samples.cpp
CSamples& GetSamples()
{
 return *SGCSamples::getInstance();
}
exe中调用:
CSamples& samples = GetSamples();
samples->GetSamples();
...

二、不用单例

可参考这里:http://stackoverflow.com/questions/10520587/how-to-implement-a-singleton-in-an-application-with-dll

通过每次讨论,从侧面也可得出,不要在接口中返回vector,包装下(如放入CSamples类中)后,获得实例,然后由实例返回STL更好

 而对于单例的实现,这里有详细的讨论:

http://www.cppblog.com/dyj057/archive/2005/09/20/346.html

本文为原创-转发请注明连接:

对于以上实现,如果有更好的其他实现,请网友们提供,先谢谢各位!