Click here to Skip to main content
15,890,741 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionCreate an incoming connection RAS server with C++ Pin
Member 800297323-May-12 12:56
Member 800297323-May-12 12:56 
AnswerRe: Create an incoming connection RAS server with C++ Pin
Erudite_Eric23-May-12 22:09
Erudite_Eric23-May-12 22:09 
GeneralRe: Create an incoming connection RAS server with C++ Pin
Member 800297324-May-12 5:55
Member 800297324-May-12 5:55 
QuestionCTabCtrl and CRuntimeClass - is there a better way? Pin
Vaclav_23-May-12 6:08
Vaclav_23-May-12 6:08 
AnswerRe: CTabCtrl and CRuntimeClass - is there a better way? Pin
Aescleal23-May-12 6:47
Aescleal23-May-12 6:47 
GeneralRe: CTabCtrl and CRuntimeClass - is there a better way? Pin
Vaclav_23-May-12 7:42
Vaclav_23-May-12 7:42 
GeneralRe: CTabCtrl and CRuntimeClass - is there a better way? Pin
Vaclav_23-May-12 10:29
Vaclav_23-May-12 10:29 
GeneralRe: CTabCtrl and CRuntimeClass - is there a better way? Pin
Aescleal23-May-12 23:46
Aescleal23-May-12 23:46 
At the moment you do your switching on type as:
C++
CView *pChild_View = (CView*) pWnd->GetWindow(GW_CHILD);
CRuntimeClass * pRuntime = pChild_View->GetRuntimeClass();


When you do the GetWindow the pointer you get back is (before you cast it, ugh) a pointer to an ordinary CWnd. You can call any CWnd virtual functions implemented in your class (and CView) without doing any casting.

Now... What you've got presumably is some sort of inheritance going:
C++
CWnd->CView->YourClasses
What I'm suggesting is that you interpose another class between CView and YourClass:-
C++
CWnd->CView->YourBaseClass->YourClasses
with a pure virtual function in it called something like:-
C++
virtual void Disconnect()=0;
Then you'd implement it in your derived classes. Hack out the bits in the if(runtime class name) blocks and stick them in there.

Now... The next problem is, when you've only got a CWnd pointer (from GetWindow() ). The first option is a C-style cast. Don't do that, it's very 1990s and unsafe in so many ways. Instead use one of two C++ casts. Use static_cast if you're sure that the result from GetWindow is one of your view classes. If it's not it can go horribly wrong and the program will crash messily. However it's really fast - it generally doesn't generate any code for single inheritance. The second choice is dynamic_cast. Use this if the result from GetWindow isn't always an object of one of your view classes. It's slower but writing UI code you won't notice it. In this case I'd say you'd be safe using static_cast as you're creating the views and ought to know what classes they are.

Anyway, having bored you with that lot, how do you replace your if() code with the virtual function calls? It'd go something like:
C++
CWnd *viewWindow = pWnd->GetWindow( GW_CHILD );
YourBaseClass *myBaseView = static_cast<YourBaseClass *>( viewWindow );
myBaseView->Disconnect();
Your'e getting a window pointer, casting it to the base class of your views then calling the virtual function through that pointer.

I'd be tempted to pile that lot in a function - it's three lines of hairy pointer manipulation you probably don't want to leave future coders. However you could always write it in a one liner:
C++
static_cast<YourBaseClass *>( pWnd->GetWindow( GW_CHILD ) )->Disconnect();
but it's a bit of a mouthful. Actually it's not too bad looking at it so maybe use the one liner.

Hope that helps,

Ash
GeneralRe: CTabCtrl and CRuntimeClass - is there a better way? Pin
Aescleal23-May-12 23:57
Aescleal23-May-12 23:57 
GeneralRe: CTabCtrl and CRuntimeClass - is there a better way? Pin
Vaclav_24-May-12 3:39
Vaclav_24-May-12 3:39 
GeneralRe: CTabCtrl and CRuntimeClass - is there a better way? Pin
Vaclav_24-May-12 7:31
Vaclav_24-May-12 7:31 
Questionnew and free Pin
pix_programmer23-May-12 0:23
pix_programmer23-May-12 0:23 
AnswerRe: new and free Pin
CPallini23-May-12 1:00
mveCPallini23-May-12 1:00 
GeneralRe: new and free Pin
pix_programmer23-May-12 1:13
pix_programmer23-May-12 1:13 
GeneralRe: new and free Pin
CPallini23-May-12 1:48
mveCPallini23-May-12 1:48 
AnswerRe: new and free Pin
«_Superman_»23-May-12 4:54
professional«_Superman_»23-May-12 4:54 
QuestionUnable to write to shared memory Pin
manoharbalu22-May-12 20:20
manoharbalu22-May-12 20:20 
AnswerRe: Unable to write to shared memory Pin
Malli_S22-May-12 20:34
Malli_S22-May-12 20:34 
GeneralRe: Unable to write to shared memory Pin
manoharbalu22-May-12 22:40
manoharbalu22-May-12 22:40 
AnswerRe: Unable to write to shared memory Repost Pin
Richard MacCutchan22-May-12 22:34
mveRichard MacCutchan22-May-12 22:34 
GeneralRe: Unable to write to shared memory Repost Pin
Richard Andrew x6423-May-12 12:08
professionalRichard Andrew x6423-May-12 12:08 
QuestionSyncronisation problem Pin
VCProgrammer22-May-12 20:13
VCProgrammer22-May-12 20:13 
AnswerRe: Syncronisation problem Pin
Malli_S22-May-12 20:45
Malli_S22-May-12 20:45 
AnswerRe: Syncronisation problem Pin
Erudite_Eric22-May-12 21:52
Erudite_Eric22-May-12 21:52 
AnswerRe: Syncronisation problem Pin
Richard MacCutchan22-May-12 22:33
mveRichard MacCutchan22-May-12 22:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.