|
Define a instance of class A.
the instance can access to a variable of class B.
could I let class A inherit class B?
|
|
|
|
|
Hi everyone,
I have included C code which offers me a lot of hard coded "extern" callbacks.
My question is: Can I encapsulate these into some kind of class?
Currently I have just many of my functions for the callbacks, but to use data arround in many of the callbacks I have to use them kind of global, and I do not really like this.
Can I set the pointers for the callback to functions that are part of a class (or my instance of it)?
Let's take an easy example that would be comparable: You want to get callbacks into the current dialog's CDialog object. You receive a string and want to show it on the dialog. Would be silly to have a global handle to the window and to use this. Or to have a global string variable if another callback should work with it later on.
Could you not set a callback pointer to one of it's functions in a way like CP_Action=this.myFunction(), so I have not to mess arround?
Or can I at least bring some straightness into it without using classes? Suggestions would be great.
Thanks in advance!
|
|
|
|
|
No, callbacks cannot be in classes, they must be global.
The reason is very simple - when you use a callback function you use it's address and the address cannot exist until there is an instance of the class plus the instance of the class cannot exist until it has been created during execution.
Elaine
The tigress is here
|
|
|
|
|
Trollslayer wrote:
No, callbacks cannot be in classes
Callbacks can be in classes as long as the function is static . By making the function static you are effectivly making the function global.
As for member functions check out:
http://www.codeproject.com/cpp/FastDelegate.asp
This will give you a good idea of how member function pointers (which are my determination callbacks anyway) work.
regards,
Rich
"Programming today is a race between software engineers striving to build bigger and
better idiot-proff programs, and the Universe trying to produce bigger and better idiots.
So far the Universe is winning." -- Rich Cook
|
|
|
|
|
Uff, a lot to read in the article about the member function pointers...
But I think this is exactly what I was searching for! Thanks a lot.
Kind regards,
Roland
|
|
|
|
|
|
Hi,
I used property sheets to display some texts. When I click the OK button then the function is normal - i.e. the dialog box closes normally. However if I press 'Enter' key to close the dialog box, I notice that the dialog box closes, but also sends a WM_KEYUP message. Is there any way I can avoid this?
I want even the 'Enter key' to function like clicking the 'OK' button.
Also I would like to know if there is any method to trace the messages? Everytime I try to it goes to the MFC source program which i dont desire.. I want to trace in my program itself.. is it possible?
Thanks.
ani
|
|
|
|
|
How would a WM_KEYUP message affect the scheme of things? Also note that when you click the OK button you do get the WM_LBUTTONDOWN and WM_LBUTTONUP messages.
|
|
|
|
|
Hi,
then what is the difference between clicking OK button and pressing the ENTER key? In the sense, I call this property sheet from a parent window. So after clicking OK or CANCEL in the property sheet, it closes and returns back to the parent window. Does it still carry the WM_LBUTTONDOWN OR WM_LBUTTONUP to the parent window? Also, what message is sent when pressing the ENTER key instead of clicking OK button directly? bcos these two seem to call different procedures in my case..
Thanks
ani
|
|
|
|
|
By default, pressing the enter key has the effect of clicking the OK button. You needn't worry about the mouse/keyboard messages that are sent. Similarly, clicking the ESC key has the effect of clicking the Cancel button.
|
|
|
|
|
Add OnOK and OnCancel Handlers in the dialogs (propertypages) to call the OnOK and OnCancel of their parent ( i.e. the PropertySheet ).
Rahim Rattani
|
|
|
|
|
In my property page, I call PropertyPage::OnOK() in its OnOk() function. I guess this will take care of the Property sheet onok?
Is there any method where I can prevent VK_RETURN to be passed on to the active window after the dialog box closes? This happens when i press Enter key to activate the OnOK() of the property sheet/page.
Since this VK_RETURN message is also passed on to the active window after destroying the property sheet, the data in the active window gets disrupted as though someone had pressed an enter key there- though they havent actually....
Any solution?
ani
|
|
|
|
|
does anybody know how to convert from "CString" to "LPCWSTR" ?
i am using BITS in my program and the "IBackgroundCopyJob::AddFile" function takes input only as
HRESULT AddFile(LPCWSTR pRemoteName,LPCWSTR pLocalName);
i wish to pass CString taken from the user interface to the BITS function. didn't get much help from MSDN
|
|
|
|
|
I found MSDN to be quite informative.[^] Don't forget to call ReleaseBuffer when you finish with it.
I take it back, you're trying to create a wide string. Try this[^] instead.
A _bstr_t will convert between narrow and wide strings for you ( by using these macros ), but is not of much use otherwise.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
|
The easiest way would be to compile you program to use UNICODE strings.
In Project - Settings - C/C++ : Preprocessor definitions
make sure _UNICODE is defined and not _ANSI or _MBCS.
This will make CString use wide chars and allow an auto cast down to LPCWSTR.
...cmk
Save the whales - collect the whole set
|
|
|
|
|
I tried to install opengl and glut on Visual C++ on dotnrt 7.1. I searched the net and did exact;y what they said, but seems like it is not working . It is really frustrating, anyone has any idea what is going on and how I should install it?
|
|
|
|
|
Hi everybody. Here's an easy question for anybody that started learning Visual C++ correctly, from bottom to top (not like I did). I want to use GetSystemTime to create a string nicely formatted with the current hour. Unfortunately, I'm totally wasted with Visual C++ string manipulation routines. Maybe you can give me a hand. I want to use chars. Here's what I got so far.
SYSTEMTIME cmTime;<br />
char *a = (char *) malloc(256);<br />
char *b = (char *) malloc(256);<br />
char *c = (char *) malloc(256);<br />
GetSystemTime(&cmTime);
I presume I could use strcat or some function to concatenate the members of the cmTime SYSTEMTIME structure in a nice string. But so far my attempts resulted only in critical errors and program crashes : D. I suppose this could be done easier using the CString class, but I want to try with this because I could learn more. Also, I think one or two char variables are enough. If you can do this with only 2 of the variables declared, it would be even better.
I would want to do something like this: "HH:MM:SS DD MM". That kind of time format. Anyway, even HH:MM is good, I'll figure the rest out. I just need to know how to work with these strings. Pf. Hard for me, I don't know where to start!
|
|
|
|
|
Axonn Echysttas wrote:
char *a = (char *) malloc(256);
char *b = (char *) malloc(256);
char *c = (char *) malloc(256);
I thought you said you were using C++ ? This is C.
In any case, the right place to start is sprintf, if you are using C. If you're using C++, I'd use a strstream, or CString.Format.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
Hi Christian. Well, I presume it might be C. But I don't even know that well the difference between C and C++.... I would like to know how to use strcat and then a messagebox to display the string. I wrote that in Visual C++. But it crashes when I try to display the result.
For example, this crashes.
a = (LPSTR)cmTime.wHour;<br />
MessageBox(0, a, "Test", 0);
And this too.
a = (LPSTR)cmTime.wHour;<br />
b = strcat(a, (LPSTR)cmTime.wMinute);
I'm really a beginner in working with strings but I would really appreciate some help. And even if this is C, I want to know how it's done in C. This is probably the hardest. If I get it like this, I'll understand more later.
-= E C H Y S T T A S =-
The Greater Mind Balance
|
|
|
|
|
Axonn Echysttas wrote:
But I don't even know that well the difference between C and C++....
Well, suffice it to say that it looks like you're reading a book on C, which means you're learning a lot of bad habits, if you intend to program C++. malloc and free are C, new and delete are C++. You should never mix them, so the easiest thing is to always use new and delete.
Axonn Echysttas wrote:
a = (LPSTR)cmTime.wHour;
You can't just cast a number to a string. If you must use C functions/want to do things the hard way, then sprintf is what you want, as I said.
Axonn Echysttas wrote:
I'm really a beginner in working with strings but I would really appreciate some help.
The core issues regarding strings:
A string is a pointer, not a single intrinsic type. As such, you need to write memory management code to create and dispose of strings.
You cannot turn something into a string by casting it.
My main beef with the way C++ is taught is that people are taught to do exactly what you're doing, hack around with functions you'll never use again, because you will use CString or ( even better most of the time ) std::string.
Oh, one more - strings are null terminated, so you need a string length of 10 to store a nine character string ( you need room for the null at the end ).
This doesn't even begin to address Unicode strings, etc. I'm just talking about a basic char * here.
Axonn Echysttas wrote:
And even if this is C, I want to know how it's done in C.
Yes, unless what you've written is in a class, everything you're doing is C, as well as being valid C++.
The core answer was in my first post - you need sprintf. I think it's a good thing for you to do it the hard way at some point, just like I think you should write your own list class before using std::list. But it's a tool for learning how stuff works, you won't write production code this way. If I had to do this in the real world, I'd write a SYSTEMTIME inserter for iostreams. But that doesn't mean you're doing anything wrong, you need to walk before you crawl. It's just that I recommend learning C++ first, with the string class, then going back later when you understand the rest to understand what the string class does for you ( because string handling is the major hump in early C++ understanding IMO )
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
Axonn Echysttas wrote:
But I don't even know that well the difference between C and C++....
See here.
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
|
|
|
|
|
First of all, the members of the SYSTEMTIME structure are the actual values, not strings. The following will construct a string according to the format you requested ("HH:MM:SS DD MM"):
char string[32];
SYSTEMTIME cmTime;
GetSystemTime(&cmTime);
sprintf(string,"%02hu:%02hu:%02hu %02hu %02hu",
cmTime.wHour,
cmTime.wMinute,
cmTime.wSecond,
cmTime.wDay,
cmTime.wMonth); You can do the same thing with CString :
CString string;
SYSTEMTIME cmTime;
GetSystemTime(&cmTime);
string.Format("%02hu:%02hu:%02hu %02hu %02hu",
cmTime.wHour,
cmTime.wMinute,
cmTime.wSecond,
cmTime.wDay,
cmTime.wMonth); Note the format: %02hu produces a 2 digit value from a WORD argument (which is unsigned, hence the 'u').
Software Zen: delete this;
|
|
|
|
|
|
Thank you Christian, Gary and Alok. Your advices are exactly what I needed to understand the issue. Gary's code is a perfect example and Alok's links will undoubtely shed light in these matters.
Christian, I knew that strings are pointers. I'm not *THAT* much of a beginner : ). There is a reason why I was doing this the C hard way. I would like to keep my Application as portable as possible. As far as I know CString is a class related to MFC right? Or do Linux versions of C++ also have this class implemented?
Nevertheless, I did do some mistakes and confusions for sure. Thank you for showing me the way.
-= E C H Y S T T A S =-
The Greater Mind Balance
|
|
|
|