|
Great! Its working now.
Thanks and Regards
|
|
|
|
|
"Every Little Smile can touch Somebody's Heart...
May we find Hundreds of Reasons to Smile Everyday... and
May WE be the Reason for someone else to smile always!" (ICAN)
|
|
|
|
|
You've already posted this in the C# forum, please don't cross post...
|
|
|
|
|
Ok, i just cannot get it. How can i nitialize static class members? I got such situation:
class myClass
{
private:
static myClass *pThis;
public:
.................
};
Now i am trying to initialize pThis to "this" so i can use it in some static methods, like threads and so.
If i am trying to initialize it in constructor, compiler says that pThis is unresolved. Ok then, i am trying to initialize it to NULL outside a class, like this:
myClass *myClass::pThis = NULL;
Now linker says that pThis is already defined in myClass.obj. What am i doing wrong?
Thanks
011011010110000101100011011010000110100101101110
0110010101110011
|
|
|
|
|
In C# one would use a "static constructor", which runs once, when the class gets referenced for the first time. However there is no such thing in C++, the best you can do is probably what is explained here[^].
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
Yeah, that issue appeared because i have this initializer in .h file, not in .cpp
011011010110000101100011011010000110100101101110
0110010101110011
|
|
|
|
|
My previous message was incorrect; this works:
In your header file you have the class definition so:
class myClass
{
private:
static myClass* pThis;
public:
myClass();
};
in your implementation (.cpp) file you have:
myClass* myClass::pThis;
myClass::myClass()
{
pThis = this;
}
The best things in life are not things.
|
|
|
|
|
Yep, that is what i needed
011011010110000101100011011010000110100101101110
0110010101110011
|
|
|
|
|
..on the other hand, if you want to use it in thread functions it would be better to send it as a parameter to the thread function.
|
|
|
|
|
That is correct. Just wanted to avoid a lot of params and have one static instance for use everywhere
011011010110000101100011011010000110100101101110
0110010101110011
|
|
|
|
|
Then my guess was right, your construct is indeed meant as a way to implement a singleton.
This is wrong on so many accounts:
1. If you deal with multiple threads, this won't be thread safe
2. By making all your functions static, you give the false impression that these functions will not modify the internal state of your program, while in truth several effectively global parameters are being modified. Anyone else seeing and trying to use these functions may quite easily introduce fatal errors into the system.
3. Since your constructor creates the instance that you need, if that constructor is being called multiple times, your pointer will be overwritten, and you lose the current state of your application irrevokably.
4. Replacing function parameters with global variables is bad design. It will lead to functions producing different results with same parameters, making them effectively untestable.
|
|
|
|
|
Stefan63, that is just wrong. Trust me i know what i am doing. One question: how would you implement custom control class by making it singleton while you need , lets say 10 instances of that class? you got 10 buttons on your dialog - now make this singleton. Second, have you ever heard of "static trampoline" ? Try this: encapsulate WndProc, CreateThread and SetWindowHookEx in a class - you will get idea. In case of mfc, encapsulating wndproc is actually out of the question, but that is not the point. Cheers
011011010110000101100011011010000110100101101110
0110010101110011
|
|
|
|
|
Now what is it - in your previous post you stated you want one static instance, now you talk of multiple instances.
You further stated that it's purpose is to save you the need to pass more parameters. And that I consider bad design. Therefore I'd not use a singleton to start with.
Of course, if these premises are wrong, then this may invalidate my arguments. But if the solution provided above works for you, and you don't think my advice was helpful, then there's no point to continue the discussion.
|
|
|
|
|
csrss wrote: so i can use it in some static methods
If you want to use static member functions or access static variables in your class, you do not have to use a static pointer or instance of that class!
When a member function is static, it means that it does not access any data members of the class or its base class(es), and also it does not use any member function that isn't also static. Therefore you do not need an instance of the class at all!
Here is an example:
class CMyClass {
private:
static int myMagicNumber;
public:
static void magic() {
std::cout << "My magic number is: " << myMagicNumber << std::endl;
}
};
CMyClass::myMagicNumber = 111;
int main() {
CMyClass::magic();
}
As you can see I never created an instance anywhere; it isn't needed.
|
|
|
|
|
How about such example:
DWORD __stdcall xxx::Monitor(LPVOID lParam)
{
HINSTANCE hExe = ::GetModuleHandle(NULL);
if (!hExe) hExe = ::LoadLibrary((LPCSTR)lParam);
if (!hExe) ::ExitThread(-1);
m_pThis->m_hMonitorHook =
::SetWindowsHookEx (WH_MOUSE_LL, MsgMonitorStatic, hExe, 0);
if(m_pThis->m_hMonitorHook == NULL)
{
::ExitThread(-1);
}
m_pThis->MonitorMessagePump();
UnhookWindowsHookEx(m_pThis->m_hMonitorHook);
return 0;
}
....
m_hMonitorThread = ::CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)Monitor, whatever,
CREATE_SUSPENDED, &m_dwMonitorThreadId);
MsgMonitorStatic also uses m_pThis to access class members and functions, modify them, call them, check them? Here i gave small example, in my project there are hundreds of lines of a code of methods which call another methods.
011011010110000101100011011010000110100101101110
0110010101110011
|
|
|
|
|
I can see no purpose the variable m_pThis serves, and I do not know why it is considered neccessary here. Nor do I see anything that's actually static. In fact, since you said that stuff is being modified, the member functions cannot be static. So I don't know why you think this is in any way relevant to your problem.
The question is what do you want to achieve? As long as we don't know that we cannot advise you how best to implement it.
P.S.: This is just a guess, but it's possible that in the code you've posted the pointer m_pThis is meant as a means to implement the 'Singleton' design pattern. If so, then this has nothing to do with the usage of static methods or static variables: static members require no instance at all, like in the code example I gave above, whereas a singleton requires exactly one instance.
Also I am under the impression that such a construct isn't well suited to implement a singleton. But that is just an IMHO, and only a guess, since there is too little code to really get the picture.
modified on Monday, May 16, 2011 12:25 PM
|
|
|
|
|
Hey guys. First things first, i am using MFC.
Ok, the question is, what is the best way to create a new window so this new window will not receive a focus?
Its like this: i got a simple dialog box app and a button on it. When i click a button, it creates a new window, without any caption, just a popup window (WS_POPUP). But this little popup wnd gets focus and i dont want this. I want it be created but the focus should stay on Dialog box window. Besides, if i move a mouse or click on this popup window - it should not receive a focus, it should be kinda "focusless" - no matter what you do - it should not receive any focus ever. How this can be done?
Edit: Maybe i should rephrase a little bit: this popup sets itself as foreground window, dialog box window should stay as foreground, not popup window
Thanks
011011010110000101100011011010000110100101101110
0110010101110011
modified on Sunday, May 15, 2011 8:33 AM
|
|
|
|
|
isn't that what enabled/disabled is for? Not sure how to do it in MFC, but that is what I would have a look at.
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
Nope.
011011010110000101100011011010000110100101101110
0110010101110011
|
|
|
|
|
Hi,
Your popup should handle WM_ACTIVATE with:
if (wParam != WA_INACTIVE)
::SetActiveWindow(::GetParent(GetSafeHwnd()));
cheers,
AR
When the wise (person) points at the moon the fool looks at the finger (Chinese proverb)
|
|
|
|
|
Thank you!
011011010110000101100011011010000110100101101110
0110010101110011
|
|
|
|
|
Hi all,
I have created one application,where i am incrmenting and decrementing values using CSpinControl.
It is incrementing and decrmenting.
Now i want to increment and decement the same using keyboard up and event press using keyboard.
void CTestDlg::OnDeltaposSpinTest(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_UPDOWN* pNMUpDown = (NM_UPDOWN*)pNMHDR;
if (pNMUpDown->iDelta == 1)
{
m_strvalue--;
UpdateData(FALSE);
}
else
{
m_strvalue++;
UpdateData(FALSE);
}
*pResult = 0;
}
void CTestDlg::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags)
{
m_strvalue--;
UpdateData(FALSE);
CDialog::OnKeyUp(nChar, nRepCnt, nFlags);
}
void CTestDlg::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
m_strvalue++;
UpdateData(FALSE);
CDialog::OnKeyDown(nChar, nRepCnt, nFlags);
}
but it is not doing it.
can anyone help me.
Hi,,
I am sharan.Working as a software Engineer in Indo-Fuji Software Company located in BTM Layout.Bangalore.India.
I have Completed my B.E(COmputers)in 2006.ANd I am having 2 years of Exp in VC++.
thanking you
sharan
|
|
|
|
|
You do not need to write a single line of code.
Just set the Arrow Keys property of the spin control to True .
|
|
|
|
|
...and it is probably not working because the dialog doesn't have keyboard focus....a control on the dialog probably does.
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
How to edit text in CListCtrl?
I am using InsertItem() but every time its adding new item in list control.I need to modify existing item.
I am using report style.
|
|
|
|