Singleton Help!
I would like to make a global object from class Print.
the object name is print, and it has some methods like print.header
print.body
print.tail
print.cookies
Thanks.
[Edited by - Tradone on March 27, 2006 5:52:40 PM]
i went through wikipedia.
it's too complex method for me, i'm new to oop design.
i just got started with classes last week.
is there a way?
this is what my main looks like
there is only 1 object created for all those objects above.
and only will be 1.
inside Print is a method that can be called by print.Parameter.
but I can't do that when I'm out of scope from the main {}
for example, when path.SetPath() is executed, inside the SetPath() method is a print.Error(); method. but since the print object has not been passed, ( and I can't pass it to all 30 classes ) the compiler keeps complaining.
another example, I have another class Skin,
and inside the skin object, I must somehow be able to call print.Parameter();
and print.Parameter how can this be done? Just an easy quick and dirty way using classes can do. Thanks.
it's too complex method for me, i'm new to oop design.
i just got started with classes last week.
is there a way?
this is what my main looks like
int main(){ Print print; Cookie cookie; Parameter par; par.SetFieldData(); Path path; path.SetPath(); path.Print();....}
there is only 1 object created for all those objects above.
and only will be 1.
inside Print is a method that can be called by print.Parameter.
but I can't do that when I'm out of scope from the main {}
for example, when path.SetPath() is executed, inside the SetPath() method is a print.Error(); method. but since the print object has not been passed, ( and I can't pass it to all 30 classes ) the compiler keeps complaining.
another example, I have another class Skin,
and inside the skin object, I must somehow be able to call print.Parameter();
and print.Parameter how can this be done? Just an easy quick and dirty way using classes can do. Thanks.
thanks.....
I need a way to pass objects automatically so that the print object automatically stays in certain listed classes.
I need a way to pass objects automatically so that the print object automatically stays in certain listed classes.
Quote:Original post by Tradone
screw it. i'm gonna pass everything manually.
thanks for the uneffort guys.
Im still confued what exactly you want... Do you want a single copy of the class shared between various other classes? If so, the only way is too use a singleton AFAIK. I personally am not too fond of singletons, so I prefer just passing pointers to objects only when thy are needed.
Also, If you want help, you are going to have to wait more than 2 hours for it...
In some header that's included everywhere that you want to use print:
extern Print print;
In one source file:
Print print;
Just like any other global. That'll let you access print from anywhere that includes the header. However, this is a horrible thing to do. This is exactly what singletons are for, as the AP suggested.
extern Print print;
In one source file:
Print print;
Just like any other global. That'll let you access print from anywhere that includes the header. However, this is a horrible thing to do. This is exactly what singletons are for, as the AP suggested.
yes, looks like singleton is the only way.
thanks, i'm reading the article here: http://gethelp.devx.com/techtips/cpp_pro/10min/10min0200.asp.
Thanks, neverk new about singleton.
and looks like it's not as complex as i thought. ( idk maybe it is.. )
thanks, i'm reading the article here: http://gethelp.devx.com/techtips/cpp_pro/10min/10min0200.asp.
Thanks, neverk new about singleton.
and looks like it's not as complex as i thought. ( idk maybe it is.. )
You don't really need to use a singleton, you could just pass it like this
Just pseudo code
or you could just access the global from the constructor
Path::Path()
{
m_pPrint = &g_print;
}
Just pseudo code
class Path{public: Path(Print *print); ~Path(); SetPath(); .... .... Print *m_pPrint;};Path::Path(Print *print){ m_pPrint = print;}Path::SetPath(){ ...... m_pPrint->Error(); ......}Print g_print;void main(){ Path path(&g_print); path.Print;}
or you could just access the global from the constructor
Path::Path()
{
m_pPrint = &g_print;
}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement