global structure; access between dialogs
Hello
am not sure an elegant way to this, so i hope somebody can give some input :o
I have a msg structure, when the application starts message are read from the file and stored in the structure. This message will be used in the main dialog as well as other modal(child) dialog.
I declared my structure in Struc.h, and tried to make an instance of the message structure but i got a LNK2005 error.
So what I did right is that after the message are retrieved, each dialog is set with the messages. So each dialog has a message structure class member.
or I can declare an all powerful structure that all dialogs can access ?? :confused:
[678 byte] By [
ideru] at [2007-11-19 16:05:16]

# 1 Re: global structure; access between dialogs
I'm afraid I didn't get you exactly. What I get is, you have a strucuture (message strucuture), that has to be accessed from multiple dialogs (currently two, main and its child), right?. How many instances will be there of this message structures? If one, then I think Singleton is a better choice.
So what I did right is that after the message are retrieved, each dialog is set with the messages. So each dialog has a message structure class member.
You mean that each dialog has its own message strucuture and when you get a message, you fill these message strucutures (in parent and child dialogs)?
Btw, could you elaborate in detail that what exactly do you want to achieve?
Ejaz at 2007-11-10 23:54:17 >

# 2 Re: global structure; access between dialogs
I'm afraid I didn't get you exactly. What I get is, you have a strucuture (message strucuture), that has to be accessed from multiple dialogs (currently two, main and its child), right?. How many instances will be there of this message structures? If one, then I think Singleton is a better choice.
Sorry I was confused meself. :o
Yes i have 1 main dialog and 4 child dialog,all of this dialog will use the same messages, so I want to have only one structure , Singleton as you have said. But as stupid as it sounds, I do not know how to declare this :o
ideru at 2007-11-10 23:55:22 >

# 3 Re: global structure; access between dialogs
i just read your last post only regarding singelton class .you can achieve this in two way.
1)
if you don't know singelton class concept no need to worry.A static variable can do this work for you. Just declare a static variable in your class or in structure what ever u r using. At the time of calling your class just increment the value of variable and check if it is more then 1 not to show dialog or not to proceed and when u release it just decrement the value of varibale
static variable :- used for singel time initialization
2)
and how to use singelton class here is one example to make a class as singelton class.
class MySingleton {
private:
static MySingleton *myReference;
MySingleton()
{
}
public:
static MySingleton *getInstance()
{
if(myReference==NULL)
myReference = new MySingleton();
return myReference;
}
};
or u can write like this (how to make a structure to a singelton)
struct MySingleton
{
static MySingleton* get_instance()
{
static MySingleton* instance=0;
if(instance==0)
instance = new MySingleton;
return instance;
}
private:
MySingleton(){}
~MySingleton(){}
};
up side i mention two different approact u can choose any one.
# 4 Re: global structure; access between dialogs
Sorry I was confused meself. :o
Yes i have 1 main dialog and 4 child dialog,all of this dialog will use the same messages, so I want to have only one structure , Singleton as you have said. But as stupid as it sounds, I do not know how to declare this :o
Ok, 2 solutions that I think of.
1) You can set the parent dialog pointer to the childs, such that childs can access the parent message strucuture. If its private in parent, then you can make child friends of parent dialog, such that, only the expected childs can access this strucuture, not everyone.
2) You can make it a singleton, but this brought the issue of authorized access. You may not like it to be accessed from everywhere. Some articles about Singletons, can be found here (http://www.dev-archive.com/Cpp/Cpp/cpp_mfc/singletons/), also there is an FAQ about that.
Ejaz at 2007-11-10 23:57:23 >

# 5 Re: global structure; access between dialogs
Hello ejaz and humptydumpty, thanks very many for the insights.. I try those approaches...:wave:
ideru at 2007-11-10 23:58:22 >

# 6 Re: global structure; access between dialogs
Done :D
1) You can set the parent dialog pointer to the childs, such that childs can access the parent message strucuture. If its private in parent, then you can make child friends of parent dialog, such that, only the expected childs can access this strucuture, not everyone.
I forgot that I did this kind of approach before, I guess I just need to be reminded..
Thanks again pips.. :thumb:
ideru at 2007-11-10 23:59:21 >

# 7 Re: global structure; access between dialogs
Hello ejaz and humptydumpty, thanks very many for the insights.. I try those approaches...:wave:
u welcome
# 8 Re: global structure; access between dialogs
Will this class be access from various threads? Because in this simple singleton implementation is not thread-safe.
cilu at 2007-11-11 0:01:30 >

# 9 Re: global structure; access between dialogs
Will this class be access from various threads? Because in this simple singleton implementation is not thread-safe.
No only single thread. But how do you make it into thread-safe??
ideru at 2007-11-11 0:02:27 >

# 10 Re: global structure; access between dialogs
Here is a stub:
// singleton.h
class Singleton
{
static Singleton* m_instance;
static CCriticalSection m_cs;
public:
Singleton* GetInstance();
void ReleaseInstance();
};
// singleton.cpp
Singleton* Singleton::m_instance = NULL;
CCriticalSection Singleton::m_cs;
Singleton* Singleton::GetInstance()
{
if(!m_instance)
{
// enter critical section
m_cs.Lock();
// check intance again
if(!m_instance)
m_instance = new Singleton;
// exit critical section
m_cs.Unlock();
}
return m_instance;
}
void Singleton::ReleaseInstance()
{
if(m_instance)
{
// enter critical section
m_cs.Lock();
if(m_instance)
{
delete m_instance;
m_instance = NULL;
}
// exit critical section
m_cs.Unlock();
}
}
cilu at 2007-11-11 0:03:22 >
