|
Hi,
I need to update a code that was written a long time ago, and have some problem.
my question is as follow.
I have a Base class with function f1 that do something.
the Derived class ovveride function f1 and do other thing.
now I need to add to the derived class a function f2 that do exactly what Base::f1() is doing.
and I dont want to duplicate the code (of course)
is it OK to do:
void Derived::f2()
{
Base::f1();
}
or is there a better way to call the f1 from the base class?
Many thanks,
|
|
|
|
|
lune12 wrote: Base::f1();
That's how you should do it.
|
|
|
|
|
You don't actually have to implement a function to call a base class version of a function:
Derived d;
d.base::f();
will call the base class version of f - no virtual function dispatch will happen.
Oh, don't try this the other way around - strange and destructive things will happen
Cheers,
Ash
|
|
|
|
|
Actually there are scenarios where this is useful, like exposing methods from private and protected inheritance. A sort of forwarding.
class Base
{
public:
string name() const;
};
class Derived : protected Base
{
public:
string name() const { return Base::name(); }
};
Edit: The reason being of course to change the access level of a method.
|
|
|
|
|
Just a thought: As someone who prefers composition over inheritance, I can hardly think of a case using protected/private inheritance. Not sure when I used it last time. Also see Stackoverflow[^].
Cheers
/Moak
|
|
|
|
|
It's more likely to be used to open up a protected function in the base class. I've seen that a lot in MFC code and it gives me the willies:
class base
{
protected:
virtual void a();
};
class derived : public base
{
public:
virtual void b()
{
a();
}
};
You can also do it using using declarations but that's scary as well.
Cheers,
Ash
|
|
|
|
|
Begs for a redesign/refactoring of the interface, if you ask me.
|
|
|
|
|
I have actually never inherited in any other way than public as far as I can recall, but elevating the access level of a function does not need protected or private inheritance though.
|
|
|
|
|
In this case I'd almost certainly rewrite the base class to provide what I want than hack the derived class. I know there are social reasons why it may be preferable to forward calls like that but it leaves a bad taste in my mouth.
Actually the real bad taste is implementation inheritance but in the world of GUI frameworks people are very stuck in the early 90s.
Cheers,
Ash
|
|
|
|
|
Unfortunately, far from all classes can be rewritten (for several reasons other than social)
I have used this for access level elevation a couple of times (probably more than two) in about 17 years, so it's not really common, but I believe there is a use for it.
There are of course also other reasons to implement a method in terms of the base class implementation. Code generation tools does it all the time. Mostly along with ToDo comments.
|
|
|
|
|
The only time you can't rewrite a base class is when you haven't got the code for it. The rest of the reasons are all social.
Just out of interest anything you can do with inheritance (of concrete base classes) in the context of code generation you could a lot more cleanly do with composition. MFC, as one example, wouldn't be such a mess of deriving from concrete classes if they'd have done that. However there wasn't the C++ expertise around in 1991 that there is now so you can't blame them in hindsight.
Cheers,
Ash
|
|
|
|
|
I think the social bit got lost in translation. I was looking upon social as political, and not economical, which I assume now you include in social?
|
|
|
|
|
I tend to include economic reasons as social, although as a supposed engineer perhaps I shouldn't.
Cheers,
Ash
|
|
|
|
|
In MFC application, I want to play audio and video file on my dialog. Audio/video file like mp3,vob,wav,flv,wma,dat,avi etc
Is there any example available
|
|
|
|
|
|
Thanks for the link
It's working for avi and mp3 files
is there any sample for other audio/video files like dat,vob,slv
|
|
|
|
|
well.. directshow is not a format bounded architecture. You just need to have the codec for the audio/video format you need to play. Directshow's intelligent connect mechanism will do the rest for you.
|
|
|
|
|
Edit: Solved myself, after a flash of inspiration!
Just removed the "KMDF_VERSION_MAJOR=1" line from SOURCE file.
Sigh...
-------------------
Good afternoon all.
I'm trying to make a driver to run on XP. I've trawled the web, and ended up with the following ultra minimal code:
#include <ntddk.h>
NTSTATUS driverCleanupFunction (IN PDRIVER_OBJECT pDriverObject);
NTSTATUS DriverEntry (IN PDRIVER_OBJECT pDriverObject, IN PUNICODE_STRING strRegistryPath)
{
strRegistryPath = strRegistryPath;
pDriverObject->DriverUnload = driverCleanupFunction;
DbgPrint ("Iain: Driver starting\n");
return STATUS_SUCCESS;
}
NTSTATUS driverCleanupFunction (IN PDRIVER_OBJECT pDriverObject)
{
pDriverObject = pDriverObject;
DbgPrint ("Iain: Driver stopping\n");
return STATUS_SUCCESS;
}
As you can see it doesn't do a whole bunch. I do plan on adding some functionality!
But I can't run it on my XP VM (you didn't expect me to mess about on a real machine to start with?), as the DDK's build has linked it with WDFLDR.sys.
I'm not using any of the WDF functionality. It's not installed on my basic XPsp3 VM (or the real XPsp3 machine to my left).
I've downloaded the DDK pretty recently (7600.16385.1).
I suspect I just have to use some sort of "#define NO_WDF_PLEASE" constant, but I have no clue what.
Any hints?
Cheers,
Iain.
I am one of "those foreigners coming over here and stealing our jobs". Yay me!
modified on Thursday, July 22, 2010 9:03 AM
|
|
|
|
|
Hi,
I am facing problem in my project and to simulate this problem I have reproduced in sample application.
I have created a SDI MFC app with doc view architecture.
I am facing problem while Print preview.
While I close the preview Window, application hangs and after some time it crashed.
Following is the implementation of OnFilePrintPreview and OnEndPrintPreview:
void CSampleView::OnFilePrintPreview()
{
// must not create this on the frame. Must outlive this function
CPrintPreviewState* pState = new CPrintPreviewState;
// temporary remove the parent so that the DoPrintPreview() uses the mainframe
ShowWindow(SW_HIDE);
SetParent(NULL);
//AfxMessageBox("Before DoPrintPreview");
if (!DoPrintPreview(AFX_IDD_PREVIEW_TOOLBAR, this,
RUNTIME_CLASS(CPreviewView), pState))
{
//AfxMessageBox("After DoPrintPreview IF");
// In derived classes, reverse special window handling here for
// Preview failure case
TRACE0("Error: DoPrintPreview failed.\n");
AfxMessageBox(AFX_IDP_COMMAND_FAILURE);
delete pState; // preview failed to initialize, delete State now
}
//AfxMessageBox("After DoPrintPreview");
}
void CSampleView::OnEndPrintPreview( CDC* pDC, CPrintInfo* pInfo, POINT point, CPreviewView* pView)
{
pView->DestroyWindow();
SetParent(AfxGetApp()->m_pMainWnd);
AfxGetApp()->m_pMainWnd->ShowWindow(SW_SHOW);
AfxGetApp()->m_pMainWnd->UpdateWindow();
}
Please provide input on this if anybody has any idea on this.
Thanks in Advance!!
Regards,
Tony
|
|
|
|
|
Had this problem before. Dont remember exactly how I solved.
I think that, OnEndPrintPreview, call:
SetDlgCtrlID(AFX_IDW_PANE_FIRST);
I have an article, http://www.codeproject.com/KB/miscctrl/BasicAdminCP.aspx
In the utils folder, ViewPrintPreview.cpp there is code working.
it´s the journey, not the destination that matters
|
|
|
|
|
Thanks for your response.
It helped me partailly but I guess now I can solve that.
Once again thank you for your response!
Regards,
Tony
|
|
|
|
|
You´re welcome.
I spent a LOT of time making print preview work for different controls and situations.
it´s the journey, not the destination that matters
|
|
|
|
|
Hi
How to create a wrapper class for an existing class?
thanks :
|
|
|
|
|
Your question is too generic: it depends on your scenario.
Which kind of class do you want to wrap (COM object, ActiveX, ...)?
What is your target?
|
|
|
|
|
Typically what you do is to create a new class exposing methods of an interface that you are interested in. Depending of what you want to wrap, the interface might be a C header file, a COM interface or another class, etc. The wrapper class often manages resource allocation and deallocation of the exposed interface, and this is often the most valuable aspect of wrapping. You can also make it easier to use the interface by bundling methods together as you see fit, removing rarely used parameters and such.
|
|
|
|