|
Robert Edward Caldecott wrote: I can't believe I haven't come across the x = type(y); syntax before. I asked some other C/C++ old-timers here and they hadn't seen it either. Mad. Always something new to learn!
I use it all the time. I use that syntax because it looks like a constructor, rather than a cast. Easier to type than static_cast, and just as easy to search for as static_cast. It's a bit dangerous, as it works like a reinterpret_cast, but hey! I've grown up, and I don't need mommy to hold my hand. (Lots of drugs are required before I start casting HANDLEs to BYTEs, etc. Since I don't use drugs, I don't worry much about bad casts. )
--
80 percent entertainment by volume
|
|
|
|
|
Jörgen Sigvardsson wrote: I've grown up, and I don't need mommy to hold my hand. (Lots of drugs are required before I start casting HANDLEs to BYTEs, etc. Since I don't use drugs, I don't worry much about bad casts. )
You may have grown up, but I can almost guarantee that 1) you won't be working at the same place forever 2) some knucklehead will come in and think he can safely cast anything to anything and use your code as an example and 3) you will find out that not using the C++-style casts will create an inconsistent coding style in your developments.
Just an FYI.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
Zac Howland wrote: some knucklehead will come in and think he can safely cast anything to anything and use your code as an example
This is the wisest thing I've heard on CP for some time; I agree completely.
Steve
|
|
|
|
|
Stephen Hewitt wrote: This is the wisest thing I've heard on CP for some time; I agree completely.
Thank you. That statement comes from years of watching that exact situation occur time and time again.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
Zac Howland wrote: 2) some knucklehead will come in and think he can safely cast anything to anything and use your code as an example
I think casts would be the least of this knuckleheads's troubles if he doesn't understand how casts work in C++.
Zac Howland wrote: you will find out that not using the C++-style casts will create an inconsistent coding style in your developments.
Yes, and no. If I start mixing the casts, then it'd be inconsistent. I use C++ casts to indicate that something worth paying attention to is happening. For instance - downcasts (static_cast), round-ball-into-square-hole-casts (reinterpret_cast), and dynamic casts (dynamic_cast). In essence, I only to constructor like casts when dealing with integral types. In that code, it's obvious what's going on - no need to pollute the visibility with lots of text.
My 2 cents.
--
As Foretold by NostradamusLast modified: den 17 juli 2006 20:09:40 --
|
|
|
|
|
Jörgen Sigvardsson wrote: In essence, I only to constructor like casts when dealing with integral types.
I have used these types of casts for integral types myself; however, I tend to look at them more as constructors instead of casts. For example:
long l = 1234L;
short s = (short)l;
Is really just creating a short by "passing" a long into an implicitly overriden constructor. It is easier to see if you view it as objects instead of primitive types. In that sense, I agree with you, but I still use caution when doing it for one simple reason: many beginning programmers tend to be lazy and see the above code and think nothing of extending it to do the following:
class A {};
class B : public A {};
B b;
B* pB = &b;
A* pA = (A*)pB;
Which while safe in this example, can get much more complex with a different inheritance model.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
The cast long l = 1234L; short s = (short)l; is equivalent to long l = 1234L; short s = short(l); . There is absolutely no difference between them and no advantage for using 1 over another.
That said, in C++, you should always try to cast using the proper C++-style casting operator (dynamic_cast, static_cast, const_cast, reinterpret_cast, etc.) See "Effective C++" by Scott Meyers for both why to use them and which ones to use when (and which ones to avoid using).
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
Anyone have an idea how to find the RGB value of the default MFC dialog.
I am building a program and need to know what color the screen comes up as so that after I am done using the CDC->TextOut command, I can call it again with a font of the same color as the background.
Please advise.
Thank you.
|
|
|
|
|
Well, I believe that the background color is GetSysColor(COLOR_3DFACE), as long as XP Visual Styles don't come into play -- they it could be anything, including a bitmap.
You would be better served to just call dc->SetBkMode(TRANSPARENT) and just draw transparent text.
|
|
|
|
|
I was using the SetBkMode(Transparent) call, but I wanted to be able to find the color of the background so that I could clean up the screen by simply calling the same TextOut function, but with the background color to effectively "mask" away the text I already painted. The GetSysColor function seems to be working. Thanks for the information.
|
|
|
|
|
See GetBkColor(GetDC()->m_hDC)
whitesky
|
|
|
|
|
In a file i have declared a function object.I am unable to access the function with the overloaded operator in the client side.
For example:
i have in my dll file
<br />
class try_func <br />
{<br />
public:<br />
try_func();<br />
virtual ~try_func();<br />
<br />
bool operator()(int value)const;<br />
<br />
};<br />
<br />
<br />
try_func::try_func()<br />
{<br />
<br />
}<br />
<br />
try_func::~try_func()<br />
{<br />
<br />
}<br />
<br />
bool try_func::operator()(int value)const<br />
{<br />
if(value< 20)<br />
return true;<br />
else<br />
return false;<br />
}<br />
<br />
<br />
try_func export_func;<br />
And then in the client side i have tried the following variations
<br />
#include<iostream.h><br />
#include<windows.h><br />
<br />
typedef VOID (CALLBACK* LPFNDLLFUNC1)(VOID);<br />
<br />
<br />
HINSTANCE hDLL; <br />
LPFNDLLFUNC1 MyFunc1; <br />
<br />
int main()<br />
{<br />
<br />
hDLL = LoadLibrary("C:\\my_func_obj_dll\\Debug\\my_func_obj_dll.dll");<br />
<br />
if (hDLL != NULL)<br />
{<br />
MyFunc1 = (LPFNDLLFUNC1)GetProcAddress(hDLL,"export_func");<br />
if (!MyFunc1)<br />
{<br />
FreeLibrary(hDLL); <br />
return false;<br />
}<br />
else<br />
{<br />
MyFunc1();<br />
}<br />
}<br />
<br />
return true;<br />
}<br />
<br />
Am i missing something here?
|
|
|
|
|
napoleaninlondon wrote: try_func export_func;
napoleaninlondon wrote: MyFunc1 = (LPFNDLLFUNC1)GetProcAddress(hDLL,"export_func");
export_func is an instance of the class try_func. It is not a function at all. So, you will never be able to 'retrieve' it with GetProcAddress. GetProcAddress is only used to get the address of a function !
Cédric Moonen
Software developer
Charting control
|
|
|
|
|
Does that mean i need to declare the function object as follows
try_func export_func;
and then also explicitly declare the operator overloaded function associated with the function object
something like
bool operator()(int value);
and use the common extern 'C' function type to wrap the function and then call that 'C' function ?
hitherto shall thou come but not further thee
|
|
|
|
|
No, it simply means that try_func is a class and will always be a class even if you overload the operator(). You can only use GetProcAddress to retrieve the address of a global function.
But what you can do is write a function that will create and return an instance of the class. That is perfectly legal.
Cédric Moonen
Software developer
Charting control
|
|
|
|
|
OK i got it so does that mean
win32 dlls never support exporting of objects of class type?
hitherto shall thou come but not further thee
|
|
|
|
|
So you are back !
<marquee scrollamount="1" scrolldelay="1" direction="up" height="10" step="1">--[ ]--
[My Current Status]
|
|
|
|
|
Yes, back from holidays . That was awesome ! I went in Spain.
Cédric Moonen
Software developer
Charting control
|
|
|
|
|
Welcome back Cedric! can just tell me ONE thing that you will never forget about Spain? multiple things allowed!
<marquee scrollamount="1" scrolldelay="1" direction="up" height="10" step="1">--[ ]--
[My Current Status]
|
|
|
|
|
First welcome Mr Moonen,whats up?did you have a good weekends?I think Spain is nice,whats spain like?
whitesky
|
|
|
|
|
I have created a class derived from an edit control. This class manages a double value as "the value" of that edit control. Whenever the value changes, I want to send a message to its parent, so that he can act upon it.
After the message has been sent, the application crashes and I get the send/dont send message from windows multiple times (depending on how many dialog controls there are on the dialog). This only happens in release mode.
The company where I work, had something to allow me to step through the code even though it was in release mode. This allowed me to see that the crash didn't happen in the function sending the message, but after the function was done or in functions in other threads. Still, the crash only happens after sending the message and when I quote the SendMessage function call out, no crash. Also, when I send another message instead of my own(e.g. WM_KEYDOWN), no crash.
The debugger always stops at this line of assembly code in !MFC42():
"mov esi,dword ptr [ecx+4]" with an acces violation. So I guess some memory gets corrupted while sending my message and the next thread that calls a standard MFC function crashes on it. How and Why, I have no clue.
I defined the message like this: "#define EN_APPLY (WM_APP + 1)" in the header file of the edit control.
Inside the message, I sent the dialog control id of the edit control as WPARAM, to allow the parent to distinguish between multiple edits.
In the parent header file I declare "afx_msg void OnApply(int iSenderCtrlId);"
In the parent cpp file I define "ON_MESSAGE(EN_APPLY, OnApply)" (with all the rest of those macros) and ofcourse, the implementation of the function (the content of which doesnt matter, because the error also appears when the function is empty)
I have this function to change the value of the edit and inform the parent:
void CEditWithScrollBar::SetWindowValue(double dValue, bool bNotifyParent)
{
if(m_dValue != dValue)
{
m_dValue = dValue;
if(bNotifyParent) (void)GetParent()->SendMessage(EN_APPLY, (WPARAM)GetDlgCtrlID()); // if I quote out this call, there is no crash.
}
CString sText;
CString sOldText;
sText.Format(m_sDisplayFormat, m_dValue);
GetWindowText(sOldText);
if(sText != sOldText)
{
SetWindowText(sText);
}
}
I found this link on errors happening in release and not in debug:
http://www.flounder.com/debug_release.htm
Maybe its of any use to anyone who wants to help out.
Thx in advance
|
|
|
|
|
Bana in Pyjama wrote: Whenever the value changes...
So do you have a handler for EN_CHANGE ?
Bana in Pyjama wrote: void CEditWithScrollBar::SetWindowValue(double dValue, bool bNotifyParent)
When is this method called?
Bana in Pyjama wrote: ...because the error also appears when the function is empty...
This implies that SendMessage() is not the culprit at all.
Bana in Pyjama wrote: I defined the message like this: "#define EN_APPLY (WM_APP + 1)" in the header file of the edit control.
Try using RegisterWindowMessage() instead, or use a constant other than 1.
"Money talks. When my money starts to talk, I get a bill to shut it up." - Frank
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
>When is this method called?
SetWindowValue is supposed to replace the CEdit::SetWindowText() function, so it could be called anywhere. Inside the class it is also called when the enter key is pressed on the control (setting the "multi line" and "want enter" styles in the designer and handling WM_ONKEYDOWN) and when WM_KILLFOCUS is received.
the error also appears when i leave the message handler of the EN_APPLY message (OnApply) empty, but the error doesnt appears when I quote out SendMessage(EN_APPLY) or when I sent any other message than EN_APPLY.
-- modified at 11:01 Monday 17th July, 2006
BTW, thx for the quick response!
|
|
|
|
|
Bana in Pyjama wrote: SetWindowValue is supposed to replace the CEdit::SetWindowText() function...
I'm not finding it in any header files or on MSDN? Is this a VC++ v6 issue?
"Money talks. When my money starts to talk, I get a bill to shut it up." - Frank
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
Infact it's CWnd::SetWindowText()
|
|
|
|