|
just by casting the baseclass pointer to derived class pointer does not make the object that was created i.e. baseclass into a dereived class.
Its just like calling Donkey a dog does not make donkey to bark .
so you can do this.
BaseClass* pBaseClass = new DerivedClass();<br />
<br />
((DerivedClass*)pBaseClass)->functionInDerivedClassOnly();
or use reinterpret_cast
-Prakash
|
|
|
|
|
|
|
|
toxcct wrote: hey, you posted the same thing as i did 1 hour later
I did not read the other solutions
-Prakash
|
|
|
|
|
toxcct wrote: where were you ?
Me, Sleeping
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
|
|
|
|
|
|
In the example above you should use static_cast<DerivedClass*>(pBaseClass) .
It's safer (but still not 100% safe) as it will only compile if it's possible for BaseClass s to exist which are also DerivedClass s (there is an inheritance relationship between them).
Steve
|
|
|
|
|
Stephen Hewitt wrote: In the example above you should use static_cast<derivedclass*>(pBaseClass)
yes, correct. Thanks.
-Prakash
|
|
|
|
|
Hi everybody. Yet another episode of my miserable API window wrapper saga. As I told you all, I'm working at a window wrapper over API. My window wrapper will be able to create controls on a window and all controls will come subclassed directly and handleled by a higher level "window master" using a singly linked list to memorise procedure pointers and stuff like this. One of the functions of this gizmo is nice tooltip windows (a lot nicer than the Windows Standard, with my own shadow system that will work even on windows 2000 which doesn't support CS_DROPSHADOW). Maybe I'll publish it here when finished, in my usual habbit: I help those who help. I already posted an article with stuff I learned here and it won't be the last. But anyway, if my freeware application has enough success I'll probably make it open source and this is a component of that application. Well, anyway, I can't finish it anyway if I can't get rid of such problems:
I am creating a WS_POPUP window via CreateWindowEx and my own class.
I am NOT showing that window.
Within it, I am creating a WS_CHILD from class "Static" (it's practically a label for displaying text).
I show the kid.
I show the parent.
Where's the kid !??!!?
Why don't I see the kid? Maybe 'cause I created it and showed it when the parent was hidden? My next logical assumption was to send another ShowWindow(hwndKID, true) to the kid. But it still doesn't get shown. Grrrrrrrr.
Thank you in advance and sorry for the long intro... got ahead of myself ::- D.
-= E C H Y S T T A S =-
The Greater Mind Balance
|
|
|
|
|
Check that it works when both are initially visible (add the WS_VISIBLE style to the window creation calls). It it doesn't (which I suspect will be the case) then the problem isn't with dynamically showing/hiding.
Steve
|
|
|
|
|
It doesn't. I tried like you said and both windows are created with WS_VISIBLE. What does this mean? I create the parent as visible but it doesn't get shown right away.... It gets shown only after the control is created but then the control isn't visible, again.
-= E C H Y S T T A S =-
The Greater Mind Balance
|
|
|
|
|
If a window is created with the WS_VISIBLE style it will be visible as soon as it is created: No call to ShowWindow will be required. Can you show us the code in question?
Steve
|
|
|
|
|
Thanks for answering by the way :- ). Yes, I know what WS_VISIBLE does. The code is quite big and has some other functions. I will try to keep it down as much as possible not to clog the post.
1. Window is created via a call to a Build function. The Build function creates the PARENT like this:
hwndThis = CreateWindowEx<br />
(<br />
NULL,<br />
sClassName,
sClassName,<br />
WS_POPUP | WS_VISIBLE,<br />
0,<br />
0,<br />
W,
H,<br />
NULL,<br />
NULL,<br />
hHost,
NULL<br />
);
2. The kid is created like this, using a call to a CreateControl function, AFTER the Build function finished its stuff:
<br />
hwndControl = CreateWindowEx
(<br />
NULL,
"Static",
Text,
lStyle,
0,
0,
0,
0,
hwndThis,
NULL,
(HINSTANCE)hwndThis,
NULL
);
The conclusion is that I don't see the kid displayed. At all.
However, if I call the CreateControl function when clicking some other button, it works. What doesn't work is when in the same place I do
Build
CreateControl
But if I do:
Build
<now click="" some="" button="">
CreateControl <in the="" code="" of="" that="" button="">
It works.
-= E C H Y S T T A S =-
The Greater Mind Balance
|
|
|
|
|
Looks like it's because the child has 0 width and height. Also the hInstance is set wrong. Try this (but correct the hInstance ):
<br />
hwndControl = CreateWindowEx
(<br />
NULL,
"Static",
Text,
lStyle,
0,
0,
100, <br />
100, <br />
hwndThis,
NULL,
(HINSTANCE)hwndThis, <br />
NULL
);<br />
In general casting should be avoided. For example without the (HINSTANCE) cast you would get a compiler but with it you're telling the compiler "just do it" and circumventing type checking.
Steve
-- modified at 3:05 Thursday 19th January, 2006
|
|
|
|
|
Well, I'm not that much of a begginer. Sorry, I forgot to mention that I do set WIDTH and HEIGHT later. So those are valid members. However, you did help me understand the real problem. The X and Y that I set later were corrupt due to some checks I made later and because the parent wasn't visible, X and Y were something like -50000. Now I corrected them and it works. So thank you for indirectly making me solve the problem!!
About the HINSTANCE: I don't think that this is the problem because it works in some cases, as I said, when I call the CreateControl function later. However, I did take your remark in notice and will try from now on to pass HINSTANCE. I don't fully understand the difference between passing a HINSTANCE and a HWND, and at the time, I couldn't get a HINSTANCE so easily. How do you get one for a HWND anyway?
-= E C H Y S T T A S =-
The Greater Mind Balance
|
|
|
|
|
Normally you get the HINSTANCE (or HMODULE , they are the same in Win32 - there used to be a distinction in the 16 bit days) from your DllMain if you're a DLL or from WinMain for an EXE. Also see here for details on the hInstance parameter of CreateWindow . In short all the system supplied window classes (such as "STATIC") ignore the hInstance parameter because they are registered with the CS_GLOBALCLASS flag.
HINSTANCE g_hInstance;
BOOL WINAPI DllMain(
HINSTANCE hinstDLL,
DWORD fdwReason,
LPVOID lpvReserved
)
{
if ( fdwReason == DLL_PROCESS_ATTACH )
{
g_hInstance = hinstDLL;
}
}
int WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
)
{
g_hInstance = hInstance;
}
Steve
|
|
|
|
|
Yeah but the Application I'm using to test&develop my API wrapper class is MFC, I don't see any "m_hinstance" member. I didn't spend too much time searching for it either but MFC doesn't have a WinMain exposed right? I got a few things made in MFC (2 of them with an UI) but it just seemed easier to throw the HWND and since I saw it working I said "ok". I think I also saw in some examples people that passed HWNDs there.
-= E C H Y S T T A S =-
The Greater Mind Balance
|
|
|
|
|
AfxGetApp()->m_hInstance
Steve
|
|
|
|
|
You're too kind :- ). Thanks.
-= E C H Y S T T A S =-
The Greater Mind Balance
|
|
|
|
|
However, how do I get the HINSTANCE of a newly created window? You see, my window API wrapper, creates a window and obtains a HWND from CreateWindowEx. But as I create children in this window, to their CreateWindowEx I also have to specify a HINSTANCE. And from where exactly will I find out the HINSTANCE of the first window that I created, considering that I did not retrieve it via WinMain since I do not have a WinMain (I have a WndProc only). Can I specify the same HINSTANCE as I specified for the parent? Or should I do in another way? Any ideas?
-= E C H Y S T T A S =-
The Greater Mind Balance
|
|
|
|
|
The HINSTANCE you pass to CreateWindow(Ex) should be:
- The HINSTANCE of the module that called RegisterClass for the desired window class (the one you're creating); or if the CS_GLOBALCLASS class style is used,
- Whatever you like (I'd use NULL in this case).
All of the pre-registered window classes like "EDIT" and "STATIC" use the CS_GLOBALCLASS class style.
Steve
|
|
|
|
|
1. So I can specify NULL for all of these?
Button The class for a button.
ComboBox The class for a combo box.
Edit The class for an edit control.
ListBox The class for a list box.
MDIClient The class for an MDI client window.
ScrollBar The class for a scroll bar.
Static The class for a static control.
2. But still, how can I get the HINSTANCE of a window that I created using CreateWindowEx using a simple class that I created too using RegisterClassEx.
-= E C H Y S T T A S =-
The Greater Mind Balance
|
|
|
|
|
Axonn Echysttas wrote: 1. So I can specify NULL for all of these?
Yes.
Axonn Echysttas wrote: 2. But still, how can I get the HINSTANCE of a window that I created using CreateWindowEx using a simple class that I created too using RegisterClassEx.
Two choices here:
1. When you call RegisterClass(Ex) use the CS_GLOBALCLASS style. If you do this you can pass NULL for your own classes. This is the more dangerous option because as the class is global you run the risk of name clashes.
2. When you call RegisterClass(Ex) pass the HINSTANCE of the you EXE/DLL and use the same HINSTANCE when calling CreateWindow(Ex) . In this case it is the HINSTANCE /class name pair that identify the window class. Typically the HINSTANCE of the module that calls RegisterClass(Ex) is used.
Steve
-- modified at 9:29 Thursday 19th January, 2006
I misunderstood (2):
A HINSTANCE identifies a module (an .EXE or DLL). I'm not sure if you can get the HINSTANCE of a class from a HWND .
|
|
|
|
|
Yeap. Ok. It's all clear now. Thank you a lot. My final decision is that I will use the HINSTANCE of the EXE/DLL all over the place, rather than using global.
-= E C H Y S T T A S =-
The Greater Mind Balance
|
|
|
|