|
I am about to write code that will perform a dynamic_cast from Base* to Derived* in a function that got the Base* from a 'this' in a constructor.
Example :
<br />
class Base;<br />
void do_stuff( Base* );<br />
<br />
class Base<br />
{<br />
public:<br />
Base( );<br />
};<br />
<br />
class Derived : public Base<br />
{<br />
public:<br />
Derived( )<br />
{<br />
do_stuff( this );<br />
}<br />
};<br />
<br />
void do_stuff( Base* pBase )<br />
{<br />
Derived* pDerived = dynamic_cast< Derived* >( pBase );<br />
if( pDerived )<br />
do_more_stuff( pDerived );<br />
}<br />
I know using an object's virtual functions within its constructor is bad, because the virtual function table is not completely built yet. I have read about dynamic_cast stating that it can convert complete objects to complete objects, is that related to the vtable?
So, can I dynamic_cast<> a 'this' in a constructor?
----------------
Blaster
Computer game programmer and part time human being
http://www.blastersoft.com
|
|
|
|
|
Sounds like a design problem.
"But that's just my opinion... I could be wrong."
-pete
"No matter where you go, there your are..." - Buckaoo Banzi
<pete>
|
|
|
|
|
You do not need cast here, or static_cast at most. As for hypothetical bad idea of using partially constructed objects in the cast - it might not work with diamond inheritance, at least not the way intended.
|
|
|
|
|
Ufff, you're entering into quicksands here... Anyway, I think your code should work. The standard says (12.7):
5 Dynamic_casts (_expr.dynamic.cast_) can be used during construction or
destruction (_class.base.init_). When a dynamic_cast is used in a con-
structor (including from the mem-initializer for a data member) or in
a destructor, or used in a function called (directly or indirectly)
from a constructor or destructor, if the operand of the dynamic_cast
refers to the object under construction or destruction, this object is
considered to be a most derived object that has the type of the con-
structor or destructor's class. If the operand of the dynamic_cast
refers to the object under construction or destruction and the static
type of the operand is not a pointer to or object of the constructor
or destructor's own class or one of its bases, the dynamic_cast
results in undefined behavior. If you read carefully, when entering into the ctor for Derived the object already behaves as a Derived with respect to dynamic_cast .
Besides, if you're writing such a mess just to avoid calling a virtual function, then you better move to virtual because similar rules apply (and the code is a little more understandable).
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
I may or may not work, but I no longer care.
Like someone said, the design is bad. I changed things a bit.
Thank you all for you information.
----------------
Blaster
Computer game programmer and part time human being
http://www.blastersoft.com
|
|
|
|
|
Damn! I just realised that in my example, the call to "do_stuff" should have been in Base's constructor, not in Derived's.
I still don't care though.
----------------
Blaster
Computer game programmer and part time human being
http://www.blastersoft.com
|
|
|
|
|
At this time I have a window in which i draws and saves screen window as bmp.
But i want to draw shapes on a window that is hidden.
I am confused that with which device context i should draw.
It will be great if u guide me on this problem.
|
|
|
|
|
dont use a window device context as such
create a memdc compatible with the window dc bitmap and draw there
"even if my world is weird its my world" biz stuff about me
|
|
|
|
|
Yes u r right
but when creating memDC using CreateCompatibleDC(....
In parameters of this function device context with which drawing is done is also given.
So what device context shold be the parameter.
As
I am creating memDc in constructor of my view class
and getting devicecontect as
CDC *pDC=GetDC(); //step1/
//doing drawing with this pdc as pDC->Rectangle(....
//after this
CDC memDC;
memDC.CreateCompatibleDC(pDC);
//and then saving window image as bmp
but i am getting an empty image in bmp file.
If i use
HDC hdc=::GetDC(NULL) in place of step1
i get figures drawn but also the desktop imageis also painted.
So which device context should be used in step1.
Thanks for ur answer
|
|
|
|
|
mkashifkkj wrote:
So which device context should be used in step1.
The screen DC, GetDC(0), will do just fine.
|
|
|
|
|
I do a lot of drawing on "imaginary" surfaces.
You could always try something like this:
<br />
BITMAP bm;<br />
GetObject(hBmp, sizeof(BITMAP), &bm);<br />
<br />
BITMAPINFO bmInfo;<br />
memset(&bmInfo.bmiHeader, 0, sizeof(BITMAPINFOHEADER));<br />
bmInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);<br />
bmInfo.bmiHeader.biWidth = bm.bmWidth;<br />
bmInfo.bmiHeader.biHeight = bm.bmHeight;<br />
bmInfo.bmiHeader.biPlanes = 1;<br />
bmInfo.bmiHeader.biBitCount = 24;<br />
<br />
HDC TmpDC = CreateCompatibleDC(NULL);<br />
<br />
BYTE *pbase;<br />
HBITMAP TmpBmp = CreateDIBSection(TmpDC &bmInfo, DIB_RGB_COLORS, (void**)&pbase, 0, 0);<br />
<br />
HGDIOBJ TmpObj = SelectObject(TmpDC, TmpBmp);<br />
<br />
HDC dcBmp = CreateCompatibleDC(TmpDC);<br />
SelectObject(dcBmp, hBmp);<br />
<br />
BitBlt(TmpDC, 0, 0, bm.bmWidth, bm.bmHeight, dcBmp, 0, 0, SRCCOPY);<br />
<br />
<br />
<br />
DeleteDC(dcBmp);<br />
<br />
DeleteObject(hBmp);<br />
hBmp = TmpBmp;<br />
SelectObject(TmpDC, TmpObj);<br />
DeleteDC(TmpDC);<br />
|
|
|
|
|
If I use InsertMenu and
BOOL InsertMenu( UINT nPosition, UINT nFlags, UINT nIDNewItem = 0, LPCTSTR lpszNewItem = NULL );
and do it in a loop which sticks in an incremented integer i (5000, 5001, 5002, etc) in the nIDNewItem position, then when I address this item in the future say using RemoveMenu(), then since I didnt put in something like ID_MYITEM, but a number instead, can I just say RemoveMenu(5001, MF_BYCOMMAND) ? Even though I didnt have a value like ID_MYITEM for that menu item?
When you add menu items dynamically , does it make entries in the resource.h file temporarily, and deletes them when the program is done?
Appreciate your help,
ns
|
|
|
|
|
Those ID_MYITEM s are just mnemonics for plain numbers. I.e. if resource.h contains
#define ID_MYITEM 5000 then ID_MYITEM is replaced by 5 before before compilation; it is just convenient notation. resource.h is not changed in any manner when running the program. So to sum it up, it is OK to add items the way you are doing without resorting to any macros defined in resource.h .
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
Ok,
I've been reading up like crazy, and I really still don't understand the benefits of using a ReBar Vs. using a bunch of ToolBars. You still have to use the toolbars anyways and then add them to the Rebar, so what is the whole point of a rebar?
And can you have more than 1 rebar?
Any insights on this matter are extremely helpful. Thank you.
Dan Willis
|
|
|
|
|
Hi,
In ReBar you can use controls like ComboBox, EditBox, ..., Also ReBar supports various features like Bitmap background. See Hotbar for an example!
A. Riazi
|
|
|
|
|
The ReBar control (which can contain multiple bars) has different layout rules than normal MFC toolbars - ReBars cannot be floated, can overlap, and move immediately when dragged. IMHO, they are generally more difficult for users to lose or accidentally close than toolbars, but if you need the cabability to drag from one side of the window to another or to float, you'll need to implement the functionality yourself or use normal toolbars.
---
Shog9
I see the way the salesmen stare into the sun
I stood and watched them as they fell off one by one...
|
|
|
|
|
Does anyone know of a good way to replace a running executable file.
Specifically, I have a piece of client software that I want to be able to update from the server. I need some sort of mechanism to replace the executable file while it is running. Obviously, I can readily download a new binary from the server without any problems, the question is how to get it executing?
I had considered the idea of using a little proxy program with this algorithm:
If filename2 is newer than filename1 then
copy filename2 to filename1
end if
execute filename1
Using an algorithm like that, filename1 is the standard executable file for my software, and an update is simply copied to filename 2. That means that next time the proxy program is run (which is always when you start the software) it auto updates.
However, there are a few problems with this, such as the hit on startup, and the fact that you have to restart before getting the new updated software.
Does anyone have a better approach to this?
(Oh, BTW, excuse the VB in the algorithm above, I switch between so often that I forget where I am sometimes!!)
Thanks for your help
|
|
|
|
|
Hi,
Like you I want to write an application to transfer a file on the net (or LAN). Is it possible to share your file transfer code to me?
Thanks a lot, A. Riazi
|
|
|
|
|
This is what I would do:
1. When the program (filename1) starts up, it checks if there's a newer version of itself available.
2. If so, it runs another small (invisible) program, passing it the process ID (GetCurrentProcessId) as a command-line parameter. Then it exits.
3. The small program then takes the process ID and retrieves the process handle for it (OpenProcess).
4. It then waits for the process to finish (WaitForSingleObject).
5. Once that happens, it copies the new version of the program (filename2), runs it, and exits.
Regards,
Alvaro
All you need in this life is ignorance and confidence, and then success is sure. -- Mark Twain
|
|
|
|
|
There's multiple ways to do this:
1. There's a wait for reboot option in MoveFileEx
MoveFileEx(src, dst, MOVEFILE_DELAY_UNTIL_REBOOT|MOVEFILE_REPLACE_EXISTING);
2. Download the update exe and run it. The update exe can post a message to the current application telling it to quit. Once it has exited then you can replace it with the update.
Todd Smith
|
|
|
|
|
Am I in much danger doing this? I wanted to see the TRACE
results w/o attaching a debugger, so I did this:
#ifdef _DEBUG
{
typedef BOOL (WINAPI* ISDEBUGGERPRESENT)();
ISDEBUGGERPRESENT pIsDebuggerPresent=(ISDEBUGGERPRESENT)GetProcAddress(GetModuleHandle("KERNEL32.DLL"),"IsDebuggerPresent");
if (pIsDebuggerPresent && !pIsDebuggerPresent())
{
ASSERT(!afxDump.m_pFile);
CString DumpFilename;
DumpFilename.ReleaseBuffer(::GetModuleFileName(0,DumpFilename.GetBuffer(MAX_PATH),MAX_PATH)?-1:0);
DumpFilename+=".TRACE-DUMP.TXT";
afxDump.m_pFile=new CFile(DumpFilename,CFile::modeWrite|CFile::modeCreate|CFile::shareDenyWrite);
}
}
#endif
I feel uneasy about it. Besides the problem of a debugger attaching to
the process later on, it may step on other dependencies of which I am
unaware. Is there a simple tool instead that will serve to receive and
display the TRACE messages?
|
|
|
|
|
|
Hello everyone.
I'm new to C++ and I can't get this to work.
Please let me know if I'm doing something wrong besides my problem
I can not update two fields in FoxPro database
it gives me an error "Can't update. Database or object is read-only"
This is how it goes:
CDaoDatabase* pDatabase = new CDaoDatabase;
pDatabase->Open("C:\\Work\\UpdateDBF\\INPUT", //path only!
TRUE, //exclusive?
FALSE, //read only?
"FoxPro 3.0;"); //connect string
CDaoRecordset rsCUST(pDatabase);
rsCUST.Open(dbOpenDynaset, "SELECT Custnum, noemail, norent FROM [CUST.dbf]");
rsCUST.MoveFirst(); //move to the beginning so we start from the begin
rsCUST.FindFirst( "Custnum = " + strCustNum );// strCustNum is a number i pull from a text file
if (rsCUST.IsEOF != 0){//if found the record
oleTemp.ChangeType( VT_BOOL );
oleTemp.boolVal = true;
try {
// this is where it fails
// the error i get is "Can't update. Database or object is read-only"
rsCUST.SetFieldValue("noemail", oleTemp);
rsCUST.SetFieldValue("norent", oleTemp);
}
catch(CDaoException* e){
TCHAR szCause[255];
CString strFormatted = _T("The data file could not be updated because of this error: \n");
e->GetErrorMessage(szCause, 255);
strFormatted += szCause;
AfxMessageBox(strFormatted, MB_OK | MB_ICONEXCLAMATION);
}
catch (...) {
TRACE ( "*** Unhandled Exception ***" );
}
} //if (rsCUST.IsEOF != 0){
If any one know please let me know
Thank you in advance.
ilya
iluha
|
|
|
|
|
I'm looking for an example of how to change images in a CListView. The items in my list can be edited, once edited I want to change the image so the user has a visual clue that the item has been modified. When the document is saved, the orignal image will be restored.
An example of this capability is Microsofts Visual Source Safe (VSS). When you check out a file, its image (file icon) gets a red check-mark and red border. When the file is checked in, the check-mark and border are removed.
I've experimented with I_IMAGECALLBACK, but it seems the callback only happens once when the item is inserted in the list control.
I've read a little about image overlays in MSDN, but I can't find enough information to make an attempt at using them. I'm not even sure if it's the appropriate solution.
Any help is much appreciated.
Marc
|
|
|
|
|
Use LVITEM structure with SetItem member function.
It's very easy!
A. Riazi
|
|
|
|
|