|
Hi gurus,
Suppose you have some classes like these:
class BaseClass<br />
{<br />
public: virtual long getValue() {return 0;}<br />
};<br />
<br />
class SubClass1 : public BaseClass<br />
{<br />
public: virtual long getValue() {return 1;}<br />
};<br />
<br />
class SubClass2 : public BaseClass<br />
{<br />
};<br />
<br />
void SomeFun (BaseClass *someInstance)<br />
{<br />
}<br />
<br />
int main (int argc, char **argv)<br />
{<br />
BaseClass baseInstance;<br />
SubClass1 subInstance1;<br />
SubClass2 subInstance2;<br />
<br />
SomeFun (&baseInstance);<br />
SomeFun (&subInstance1);<br />
SomeFun (&subInstance2);<br />
}
In other words, in SomeFun I want to find out which implementation of getValue is used by the given instance. I don't want to actually call someInstance->getValue, but I just want to find out which method would be called if I would call it.
I tried to find the address of the method by using
&someInstance->getValue
and
someInstance->getValue
but the first one doesn't compile, and the second one gives a warning and doesn't work.
Notice I don't want to use something like
pMethod = BaseClass::getValue;
or
pMethod = SubClass1::getValue;
since I don't know who has inherited from my BaseClass, nor which instance is given as argument to me. I just want to know: the class of which an instance is given to me, has it overruled the virtual method getValue or not?
Of course a solution could be to add another virtual method, something like:
bool iHaveOverruledGetValue();
which should be implemented by each subclass (if they have overruled the getValue method, but that doesn't seem like a good solution either.
Anyone suggestions ?
Enjoy life, this is not a rehearsal !!!
|
|
|
|
|
Patje wrote:
Of course a solution could be to add another virtual method
IMHO, that seems like the best approach, both from an OO standpoint and a maintainability one. otherwise, with the pointer idea, you're writing code that is at best confusing and at worst non-portable.
-c
Support regime change. Vote.
|
|
|
|
|
You can create a switch statement and try dynamic_cast but that requries RTTI to be enabled.
I would just add a virtual method.
Todd Smith
|
|
|
|
|
is there a way to stop this from open when a certain condition is met?
I'm trying to do this in the OnDropdown section of code and anything I do dosen't stop the dropdown from opening.
Thanks for your help,
Jim
|
|
|
|
|
I assume it's for a combox box, why can't you just disable the control ?
the OnDropDown is the callback.
Max.
|
|
|
|
|
Here is what the code looks like. I couldn't find anything that would just quite without displaying that drop down.
void CAMID::OnDropdownMSel()
{
CComboBox *pMSelBox = (CComboBox *) GetDlgItem(IDC_MISSION_SEL);
if(HostStruct.bFreeze == false)
{//warn user...and quit dropdown
MessageBox("blah blah blah", "Warning!", MB_OK);
}
else
{
//drop down box
}
|
|
|
|
|
I have strings (numeric) of various lengths:
"123"
"2345"
"5"
"99999"
etc. I need to make them all as long as the longest one "99999", so they should look like "00123", "00005", etc.
If these strings are stored in an array A[], then how can I do this padding? I will be knowing the biggest strings length.
I need to automate this process.
for ( int i = 0; i
|
|
|
|
|
use CStrings if u can
CString urString;
in the loop:
urString = a [i];
CString t( '0', numberOfSpaceNeeded );
t += urString;
Papa
while (TRUE)
Papa.WillLove ( Bebe ) ;
|
|
|
|
|
Amazing! Thanks! I didnt know CString had such a handy constructor.....
Appreciate your help,
ns
|
|
|
|
|
You could format the number with the approprite lenghth
CString aString;
aString.Format( "%7i", 123 );
|
|
|
|
|
Maximilien wrote:
CString aString;<br />
aString.Format( "%7i", 123 );
You forgot the zero:
aString.Format("%07i", 123);
|
|
|
|
|
|
I write a class which kicks off a thread in constructor and terminated it in destroyer. This thread has a message queue, and in the switch-case, calls the class' member functions or member variables. And so far, I got the following reward :
illegal call of non-static member function
In my point of view, It's reasonable to use massage queue to manage member functions and member variables. it sounds good but I kick a stone, tell me why and how to make a difference.
Thanks
Extreme programming. Do the No.1
|
|
|
|
|
a thread function (the one that you use to start the thread) cannot be a member function of a class, unless that function is static.
-c
Support regime change. Vote.
|
|
|
|
|
why??? why must be static? u mean serval instances have only one thread function? then once again, how to call a member funtion in a thread function?
What's happening in backyard?
thanks
Extreme programming. Do the No.1
|
|
|
|
|
stevenson wrote:
why??? why must be static?
the thread proc can't be a non-static member function because there is no way to provide the "this" pointer to provide an object context for that member function to be called from. it's like this:
int i = CString::GetLength();
GetLength makes no sense without an actual CString object. a static member function doesn't require an actual object instance, so you can use it as a thread proc.
you can pass an object into the thread proc and use it indirectly:
volatile dataStruct data;
data.pMyObject = &TheObjectIWantToUse;
AfxBeginThread(ThreadProc, (void *)&data, THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED);
...
UINT ThreadProc(void *p)
{
dataStruct *pData = (dataStruct *)p;
now you can use pData->pMyObject;
}
-c
Support regime change. Vote.
|
|
|
|
|
Hi,
I have launch the hh.exe ( help viewer) via CreateProcess(). I am trying to close this window when my main program exits. How do I do that?
Here's a snip of my code...
bSuccess = CreateProcess ("C:\\WINDOWS\\HH.EXE"," NATURAL.CHM",
NULL, NULL, TRUE, NORMAL_PRIORITY_CLASS,
NULL, NULL, &si, &ppi );
helphnd = ppi.hProcess;
CloseHandle ( ppi.hThread );
CloseHandle ( ppi.hProcess );
The code is written as part of defining a 'Help' menuitem. Quite new to the idea of process, copy this idea from FAQ in this website. If CloseHandle is executed right after CreateProcess, that means the process should have finish and return to the executing program, therefore closehandle would do the cleaning up? Now, I am executing something in the ::OnCloseDocument(), how do I check whether the help program is still running,so that I wouldn't be trying to close the program?
|
|
|
|
|
unsigned long status;
int rv = GetExitCodeProcess(ppi.hProcess, &status);
Todd Smith
|
|
|
|
|
THanks for the reply. It's this function just checking whether the process is closed? How to close the process then?
|
|
|
|
|
|
Looks like we can only insert strings, so if i want to put in ints or floats, so I have to first atoi, atof them before inserting them? ?
Appreciate your help,
ns
|
|
|
|
|
Correct, but you need itoa or ltoa (ints to strs), not atoi (strs to ints)...
Not sure if there's a ftoa, so you may need to do an sprintf() or something similar...
Chris
"If at any time I announce that a nation or kingdom is to be uprooted, torn down and destroyed, and if that nation I warned repents of its evil, then I will relent and not inflict on it the disaster I had planned. And if at another time I announce that a nation or kingdom is to be built up and planted, and if it does evil in my sight and does not obey me, then I will reconsider the good I had intended to do for it." -- Jeremiah 18:7-10 (God, commenting on the value of the United Nations)
|
|
|
|
|
Hi,
It's possible to find a window by a incomplete name ??
Ex.: Finding a window that has the name "App Test", ::FindWindow ("App Tes?")
Other thing: How find a window by a class name ??
[]'s
Cris.
|
|
|
|
|
Cris wrote:
How find a window by a class name ?
According to MSDN, you should call FindWindow with lpszWindowName set to NULL.
rechi
|
|
|
|
|
But how I get the class name ??
I am using a dialg based app, how I registry the class name ??
|
|
|
|