|
Looks like a factory pattern is what you want.
/ravi
"There is always one more bug..."
http://www.ravib.com
ravib@ravib.com
|
|
|
|
|
|
One use of a factory design pattern is to ensure that objects are properly initialized during construction. I assume this is what you'd like to accomplish via the static constructor. To implement a factory pattern for the class CFoo, do the following:
- Make CFoo's constructor protected, thereby preventing anyone from doing CFoo* pFoo = new CFoo(); which could create an uninitialized instance.
- Implement the static method CFoo* CFoo::Create(); that serves up a properly initialized object. The static method can set the class's static member is required.
You should browse thru the gang of 4 book. I think you'll enjoy it.
/ravi
"There is always one more bug..."
http://www.ravib.com
ravib@ravib.com
|
|
|
|
|
Seems to me like a Singleton pattern would do the job better. BTW the standard (well Nov96 draft at least) says a constructor can't have a static modifier (12.1 para 4) if you want official confirmation.
i1.2sqrt(u).bcos(ur)sec(c)
but
b4.isqrt(u).ru/16
|
|
|
|
|
I think the Singleton pattern is better suited when you want to expose methods from the one and only instance of a class.
But I agree that the ClassFactory pattern can do more than serve up a single class. In fact, it's power lies in the fact that it can serve up specializations of a general base class.
/ravi
"There is always one more bug..."
http://www.ravib.com
ravib@ravib.com
|
|
|
|
|
A static constructor without a this? What would you be constructing?
Tim Smith
Descartes Systems Sciences, Inc.
|
|
|
|
|
There is no such thing. You'd do something like:
class C
{
public:
static int m_stat_var;
static int stat_func() { return 1; }
};
int C::m_stat_var = C::stat_func();
--Mike--
"There are only a limited number of jobs where they will ask to see the sausage. Most of them are in movies."
-- Christian Graus, 2/11/2002
My really out-of-date homepage
Sonork - 100.10414 AcidHelm
Big fan of Alyson Hannigan.
|
|
|
|
|
[removed]
modified 27-Nov-11 9:31am.
|
|
|
|
|
That's Just The Way It's Done. It's similar to the situation when you write "extern int n;" in a header file. You still need to actually declare the variable "int n;" in a .cpp file.
--Mike--
"There are only a limited number of jobs where they will ask to see the sausage. Most of them are in movies."
-- Christian Graus, 2/11/2002
My really out-of-date homepage
Sonork - 100.10414 AcidHelm
Big fan of Alyson Hannigan.
|
|
|
|
|
[removed]
modified 27-Nov-11 9:31am.
|
|
|
|
|
Well, I can't really answer that since I don't know C#, so don't know what you mean by a static constructor.
--Mike--
"There are only a limited number of jobs where they will ask to see the sausage. Most of them are in movies."
-- Christian Graus, 2/11/2002
My really out-of-date homepage
Sonork - 100.10414 AcidHelm
Big fan of Alyson Hannigan.
|
|
|
|
|
Nish [BusterBoy] wrote:
Why am I re-declaring the static int
You're not redeclaring it. The .h file declares it - the .cpp allocates storage for it. If you omitted it from the .cpp file you'd get an unresolved reference link error.
/ravi
"There is always one more bug..."
http://www.ravib.com
ravib@ravib.com
|
|
|
|
|
|
I think I said "picky picky".
/ravi
"There is always one more bug..."
http://www.ravib.com
ravib@ravib.com
|
|
|
|
|
You are not redeclaring it but you are just declaring it. As it is static it resides even if your class object has not been constructed. It is like using static variables in functions or global vars. IMO the compiler initialises them to zero if you don't do the essentials. So here just after your class def you should initialize it otherwise the compiler spews errors.
Atul
Sonork 100.13714 netdiva
|
|
|
|
|
|
Static Constructor...The idea is kinda weird ..How are they implemented in C#?
Atul
Sonork 100.13714 netdiva
|
|
|
|
|
I'm a not exactly a veteran in C++, but I know my way around. I want to get into Windows programming, rather than console. I've heard of SDK and MFC. What is what and which do I want and what advantages are there to each? From what I've seen, SDK is much simpler, but is it better?
|
|
|
|
|
SDK first to understand how Windows works "under the hood." Once you have a grasp of that, then learn MFC, which is a C++ OO wrapper of the Win32 API.
Sorry about the brevity of this response - gotta go see my Valentine!
Jon Sagara
What about ?
Sonork ID: 100.9999 jonsagara
|
|
|
|
|
Ok, that answers one question. But the other is, where do I learn these SDK functions? What is the search term to use? I've crawled the web for about an hour now and I can't find anything. What is a good tutorial that defines all the functions and does a good job explaining everything. Thanks.
|
|
|
|
|
Try msdn.microsoft.com
------------------------
Derek Waters
derek@lj-oz.com
|
|
|
|
|
Anonymous wrote:
But the other is, where do I learn these SDK functions?
The Win32 programming bible "Programming Windows" by Charles Petzold covers Win32 SDK.
There is a good tutorial at Relisoft
Happy Windows Programming!!
Atul
Sonork 100.13714 netdiva
|
|
|
|
|
|
If your program is GUI intensive go for MFC which makes things a lot easier. But if you are doing some winsock stuff use plain SDK. The MFC socket classes have their own problems.
Nish
Nish was here, now Nish has gone;
He left his soul, to turn you on;
Those who knew Nish, knew him well;
Those who didn't, can go to hell.
I like to on the Code Project
Sonork ID 100.9786 voidmain
www.busterboy.org
|
|
|
|
|
You should absolutely start at the API level. Don't even think about getting into MFC until you are comfortable using the straight APIs. (Yes, I know, others will disagree with me; my opinion is based on my own experience and that of other programmers I know.)
--Mike--
"There are only a limited number of jobs where they will ask to see the sausage. Most of them are in movies."
-- Christian Graus, 2/11/2002
My really out-of-date homepage
Sonork - 100.10414 AcidHelm
Big fan of Alyson Hannigan.
|
|
|
|