|
I need to define an array of CMenu's. But I really dont now how many I need, I only know this at runtime.
I cant do this... CMenu menu[myvariable];
Because it needs a constant.
When I use CMenu *menu = new CMenu[myvariable];
The program crashes when I try to access a member function of CMenu. Like, menu[0]->AppendMenu[...];
Can I not use CMenu[variable] at all? Is there no workarounds?
BTW, I dont know why it crashes when I use pointers to CMenu.
Thanks!
Daniel
|
|
|
|
|
You could use a dynamic collection (eg: CPtrArray ).
/ravi
My new year's resolution: 2048 x 1536
Home | Articles | Freeware | Music
ravib@ravib.com
|
|
|
|
|
I would use a linked list........
|
|
|
|
|
Daniel1324 wrote:
The program crashes when I try to access a member function of CMenu. Like, menu[0]->AppendMenu[...];
Forget what I first typed, it was plain wrong. However, it seems probable that your array of CMenu items needs to be initialised in some way.
Or you could use a vector, or list, depending on the metrics of how you will use the collection.
Christian
I have drunk the cool-aid and found it wan and bitter. - Chris Maunder
|
|
|
|
|
You have to initialize each CMenu in the array via a call to CreateMenu() , something like this:
CMenu *menu_array = new CMenu[myvariable];
for (int i = 0; i < myvariable; i++) {
menu_array[i].CreateMenu();
}
Software Zen: delete this;
|
|
|
|
|
Nice n simple, and it should work
MSN Messenger.
prakashnadar@msn.com
Tip of the day of visual C++ IDE.
"We use it before you do! Visual C++ was developed using Visual C++"
|
|
|
|
|
Thanks! I tried that and it worked, but I dont understand why.
I thought that with pointers you had to use '->' instead of '.'.
I was using menu_array[i]->CreateMenu();
instead of menu_array[i].CreateMenu();
Could you shed some light on this for me?
Thanks!
|
|
|
|
|
In C and C++, an array is simply a constant pointer to the first array element. Array notation A[i] denotes the i<sup>th</sup> element of the array starting at A , and is equivalent to the pointer notation *(A + i) .
Turning this around, this means you can use a pointer as if it were an array, and this is what I did in my example. The menu_array value is a pointer to the first element in an array. Given that, menu_array[i] gives you the actual CMenu object, rather than a pointer to an object.
I realize this is rather subtle. It takes a while to get used to.
Software Zen: delete this;
|
|
|
|
|
Cool, that kinda makes sense... I'll have to think about it for a while!
Thanks!
|
|
|
|
|
Daniel1324 wrote:
When I use CMenu *menu = new CMenu[myvariable];
You declare an array of CMenu so menu[n] is a CMenu, not a *CMenu
There are plenty of other reasons why the program would crash but until you understand this first one you're not going to get very far...
The opinions expressed in this communication do not necessarily represent those of the author (especially if you find them impolite, discourteous or inflammatory).
|
|
|
|
|
Hi.
Is there a way to capture a char from console buffer in a Win 32 Console Application?
I know how to place the cursor on the console screen and replace something but now I wanna know how I get the char from the cursor.
|
|
|
|
|
search for Console here in CP you will many examples that does that.
MSN Messenger.
prakashnadar@msn.com
Tip of the day of visual C++ IDE.
"We use it before you do! Visual C++ was developed using Visual C++"
|
|
|
|
|
Hi,
I am trying to migrate my existing code (c++ - MFC) that works fine in VS6 to VS7.
In my code i have a client server mechanism using the MFC CSocket and Carchive to serialize objects between sides.
This works fine under VS6, but when compiled and run in VS7 the actual serialization process (the storing\reading to\from the archive object) takes much much longer, and after a while it simply get stuck.
when i break in to the the code on the send side i see it is stuck in CSocket::SendChunk and every time it tries to send it gets the WSAEWOULDBLOCK.
(All of the transferred objects are CObject derived ( DECLARE_SERIAL, IMPLEMENT_SERIAL and Serialize overloaded) and contains only MFC CObject derived or CStrings composite object )
Any known issues or ideas?
|
|
|
|
|
im looking for a method,
to find out the account type
of the logged in user,
as in,
administrator or guest priveldges etc
Thanks
|
|
|
|
|
I am having a problem with threads and wonder if anybody can help,
I am spawning a worker thread that updates a linked list that I need access to from the other thread, basically i am passing the instance of the class with the linked list using a struct:
UINT CNetworkMonitoringDoc::CaptureThread(LPVOID pParam)<br />
{ <br />
THREADPARMS* ptp = (THREADPARMS*) pParam;<br />
CPacket* m_capturedPacket = ptp->pPacketInst;<br />
delete ptp;<br />
...<br />
}<br />
The worker thread adds data to the linked list ok but when the orginal thread needs to do something with the m_capturedPacket instance the contained linked list is empty, im guessing the thread is creating its own private copy and working on that; is there anyway to share m_capturedPacket between them?
Cheers
Andy
|
|
|
|
|
I solved this problem by placing a critical section around the usage of the instance of my class
|
|
|
|
|
Hi,
General question:
How can can I obtain a handle to some window only by using its handle, for example: 000004C8
Detailed Question:
How can I obtain a handle to a textbox which resides on a modal dialog, which itself opens as the child of its parent? I should say that I don't know what's the name of that editbox, I can just obtain its handle for example by using spy++.
|
|
|
|
|
Anonymous wrote:
How can can I obtain a handle to some window only by using its handle
HWND hWnd = 0x000004C8;
MSN Messenger.
prakashnadar@msn.com
Tip of the day of visual C++ IDE.
"We use it before you do! Visual C++ was developed using Visual C++"
|
|
|
|
|
Excuse me, I meant a pointer to its window:
CWnd *pWnd = ...
|
|
|
|
|
pWnd->m_hWnd; gives you the handle of the window.
MSN Messenger.
prakashnadar@msn.com
Tip of the day of visual C++ IDE.
"We use it before you do! Visual C++ was developed using Visual C++"
|
|
|
|
|
You can use CWnd::Attach() to attach a window handle HWND to a CWnd object.
Ant.
|
|
|
|
|
Is it possible to map a CComboBox control to a CString data type? I'm looking in the class wizard, but it only gives int as a possible value so I just want to double check. Thanks.
- Aaron
|
|
|
|
|
int is a verymuch a logical thing to get, coz you can then get corresponding string,and other values from it.
MSN Messenger.
prakashnadar@msn.com
Tip of the day of visual C++ IDE.
"We use it before you do! Visual C++ was developed using Visual C++"
|
|
|
|
|
sry to post this 2x but i figured some ppl wouldnt see it in my other thread.
i get this error:
cannot convert parameter 1 from 'unsigned int' to 'const char *'
Conversion from int type to pointer type requires reinterpret_cast, C-style cast or functions-style cast
for this line of code:
m_port.AddString(ports.ElementAt(i));
How can i fix this?
Thank you
---------------------
And Like The Wind Our Hero Vanishes Off Into The Distance...
|
|
|
|
|
It seems that ports.ElementAt if of type unsigned int (number) and must be of type const char * (string).
Could you provide the ports structure/class declaration/initialization?
|
|
|
|