|
Yes. but I believe that the state of the parameter only may throw an exception, if its state is corrupted.
that's why we do a temporary assignement (temp = a ) to check first if assigning a to something else would not crash.
so if a is OK, then swapping it with this would be OK too...
|
|
|
|
|
Well, even ignoring the recursion madness of George sample code, I think that swap may take care about, since it will perhaps be coded in a way similar to the following:
A temp;
temp = a;
a = *this;
*this = temp;
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
[my articles]
|
|
|
|
|
that's how swap is normally designed.
however, there's a lack in this design, because having a A::operator=() calling a A::swap() function, which itself calls A::operator=() 3 more times is recursive for sure, with no possibility to stop the loop.
so I think swap is not coded like this in B. Stroustrup's example...
|
|
|
|
|
toxcct wrote: that's how swap is normally designed.
The above I was suggesting: hence no need to create a temporary assignment before it.
toxcct wrote: however, there's a lack in this design, because having a A::operator=() calling a A::swap() function, which itself calls A::operator=() 3 more times is recursive for sure, with no possibility to stop the loop.
so I think swap is not coded like this in B. Stroustrup's example...
I agree that there is recursion in the sample code (after all I was the first pointing out it...) and I was not suggesting to take that code as it stands, suppose, for instance, that method name was foo , instead of operator== .
BTW I think that swap should rely on the assignment operator and not the opposite (but I've not Stroustrup's example, that of course I suppose correct, at hand, I think George is not a diligent poster recently... )
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
[my articles]
|
|
|
|
|
Thanks CPallini,
I agree that there is some issue in my code. It is appreciated if you could provide some pseudo code to correct my code to implement the using swap technique to implement a exception safe assignment operator.
regards,
George
|
|
|
|
|
George_George wrote: I agree that there is some issue in my code.
Some issue? Some issue? Some issue?
I won't call 'some issue' going into infinite recursion while aiming to provide an 'exception safe' operator.
Just kidding...
George_George wrote: It is appreciated if you could provide some pseudo code to correct my code to implement the using swap technique to implement a exception safe assignment operator.
Oh no, you made the original post: that's your side of the game.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
[my articles]
|
|
|
|
|
Hi CPallini,
I have searched some answers, but I do not think it works. Because either copy constructor or swap will potentially call assignment operator again.
Any ideas?
A& A::operator=(const A& other)
{
A temp(other);
swap(temp);
return *this;
}
regards,
George
|
|
|
|
|
hey, you want an exception safe copy ?
backup your object, then assign it the new object, and if it fails, restore its backup !
|
|
|
|
|
Thanks toxcct,
I agree with the general rule. But how to avoid the recursive invocation of assignment operator in your comments? Could you provide some code please?
regards,
George
|
|
|
|
|
toxcct wrote: hey, you want an exception safe copy ?
backup your object,
or take a picture of it.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
[my articles]
|
|
|
|
|
Hi toxcct,
I think in such pattern, swap should be implemented to a list of assignments of POD types, right?
BTW: if I am wrong, please help to correct code in your senses.
regards,
George
|
|
|
|
|
Thanks toxcct,
I agree that there is some issue in my code. It is appreciated if you could provide some pseudo code to correct my code to implement the using swap technique to implement a exception safe assignment operator.
regards,
George
|
|
|
|
|
Hi CPallini,
The code will cause recursive to invoke assignment operator?
regards,
George
|
|
|
|
|
Hi toxcct,
Two more comments.
1.
We are talking about the state of current object instance (this object), why you are talking about the state of parameter?
toxcct wrote: Yes. but I believe that the state of the parameter only may throw an exception, if its state is corrupted.
2.
I do not 100% agree. Since swap will create another new temporary object used to do swap. The creation of new temporary object may cause bad_alloc exception.
toxcct wrote: so if a is OK, then swapping it with this would be OK too...
regards,
George
|
|
|
|
|
George_George wrote: We are talking about the state of current object instance (this object), why you are talking about the state of parameter?
1) because : if the state of the parameter is corrupted, the state of the current object could have been corrupted too. but as long as we're assigning a temporary object first, the current object's assignment is exception-safe !
2) not sure for this one. isn't bad_alloc sent only when allocating memory on the heap (using new) ? here, temp is on the stack though !
|
|
|
|
|
Thanks toxcct,
In which step do you think the state of the input parameter will be corrupted? Could you provide some pseudo code please?
toxcct wrote: 1) because : if the state of the parameter is corrupted, the state of the current object could have been corrupted too. but as long as we're assigning a temporary object first, the current object's assignment is exception-safe !
regards,
George
|
|
|
|
|
Hi,
when I pass a String from VB to C++dll, the code will work, the string will appear on a MessageBox called from the Dll.
When i click Ok the following error message appears: "Wrong Dll_Calling Convention" !!!????
The Code
VB:
<br />
Private Declare Function fnWin32DLL02 Lib "E:\WORKING\OMRON\GDT\EigeneDLL\C++ DLL mit VB (Test01)\Win32DLL02.dll" (ByVal a As String) As String<br />
<br />
Private Sub Form_Load()<br />
<br />
Dim s As String<br />
<br />
s = "Übergabe aus VB"<br />
fnWin32DLL02 (s)<br />
<br />
End Sub<br />
Cpp:
<br />
WIN32DLL02_API BSTR fnWin32DLL02(LPCSTR s)<br />
{<br />
MessageBox(NULL,s,"Info",MB_OK); <br />
return CComBSTR (BSTR("abc"));<br />
}<br />
When i change the LPCSTR s to BSTR s, ...the same error appears !
Could you imagine where the error comes fom ?
Many Thanx and best regards
Croc
|
|
|
|
|
Hey Croc, have you seen my (now old) reply?
http://www.codeproject.com/script/Forums/View.aspx?fid=1647&msg=2381364[^]
You have to use __stdcall calling convention.
Furthermore you need:
(1) to use SysAllocStringByteLen("hhh", 3) to allocate the returned string
(2) use LPCSTR (as you done in the posted code) for the input string.
Good luck!
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
[my articles]
|
|
|
|
|
1000 *TH*A*N*X* PALLINI,
now it work's an no sry i was to fast, so i didn't remember the __stdcall !!!!!!!!!!!
Thanx again & best regards
Croc
|
|
|
|
|
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
[my articles]
|
|
|
|
|
This is exactly what CPallini and I were talking about when you posted about this before. The C++ side needs to have __stdcall calling convention. This makes the order parameters are put on and pulled off the stack (and which side is responsible for cleanup) compatible with VB. Otherwise your stack is trashed on the return from the call, hence the error your're seeing. You're fortunate, in the past the error would have been silent and your C++ would have blown up spectacularly the next time it made a function call .
You need to find the definition of WIN32DLL02_API and make sure it includes __declspec(__stdcall)
Nothing is exactly what it seems but everything with seems can be unpicked.
|
|
|
|
|
Hi Matthew,
i added
#define WIN32DLL02_API __declspec(_stdcall) in the definition of WIN32DLL02_API, but without this the code worked too !???
Best regards
Croc
|
|
|
|
|
Hi friends,
I want to send my dialog box to the system-tray when "WINDOWS key + D" is pressed(Show Desktop),
but I don't know which message is sent.
I have captured all messages that has sent to my dialog by SPY++, but none of the is acceptable.
Since my dialog has no border (it has no caption) and subsequently it has no minimized property in the dialog properties, I don't know how to handle the message.
By the way, I handled "OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized)" message, but the message is sent for Activating and Inactivating.
But I want to handle a message which is sent when "Windows key+ D" is pressed merely.
Thanks.
|
|
|
|
|
you don't have to capture the message.
you just have to design your application so that it is minimized to the System tray.
same would happen then if you manually minimize it (with the upper right buttons), if you press the task bar button to reduce it, if you press Win+D (show Desktop), or Win+M (minimize all)...
|
|
|
|
|
Would you mind explaining me how can I design my application to send the dialo to the System tray automatically by minimizing the Windows (Win+D Or Win+M)?
|
|
|
|