programming interfaces

Started by
15 comments, last by doynax 19 years, 5 months ago
Quote:Original post by guyaton
I'm still not fully sure, would you mind a real simple demo?

thanks.
I've never written a single line of COM code in my life, so you're better of searching for tutorials ;)

But I can at least illustrate the basic principle with some source.
// Public interface class with only pure virtual functions. This is what the users seesclass ITimer {public: virtual int Query() = 0;};// The actual implementation. This is the hidden class that does all of the workclass TimerImpl : public ITimer {private: int Base;public: TimerImpl() { Base = time(NULL); } int Query() { return time(NULL) - Base; }};// And a create function is also exported since the users need a way of creating the hidden TimerImpl'sITimer *CreateTimer() { return new TimerImpl; }
There's also some complete sample code for implementing a context menu on the page I previously linked to (#1 and #2).
But what you really need is a tutorial.
Advertisement
It was a nice try, however inheriting an Interface apparently makes the inherited class abstract...oh well thanks for the try! where's those microsoft coders? i'm sure they'd solve this in 1/2 a second.

if your curious this is what i did (for anyone else out there who might possess the answer):

[uuid(336389EB-B3BA-4278-9107-28EA51F552DA)]typedef __interface IParserFile : public IUnknown{public:	IParserFile();	virtual HRESULT LoadFile( [in, string] char *strFileName ) = 0;}IParserFile, *LPPARSERFILE;class CParserFile : public IParserFile{public:	CParserFile()	{		;	}};#ifndef ParserFileCreatevoid ParserFileCreate( LPPARSERFILE *ppParserFile ){	*ppParserFile = new CParserFile();}#endif


error is:
error C2259: 'CParserFile' : cannot instantiate abstract class


~guyaton
Quote:Original post by guyaton
It was a nice try, however inheriting an Interface apparently makes the inherited class abstract...oh well thanks for the try! where's those microsoft coders? i'm sure they'd solve this in 1/2 a second.

if your curious this is what i did (for anyone else out there who might possess the answer):

*** Source Snippet Removed ***

error is:
error C2259: 'CParserFile' : cannot instantiate abstract class
I think you have to implement all of the pure virtual methods in IUnknown (QueryInterface, AddRef and Release).
thanks! that worked!!!....but i have to do impliment everyting in this other class? doesn't that defeat the purpose of interfaces?

[Edited by - guyaton on October 23, 2004 4:58:25 PM]
~guyaton
Quote:Original post by guyaton
thanks! that worked!!!....but i have to do impliment everyting in this other class? doesn't that defeat the purpose of interfaces?

Which class are you refering to?
It "should" be enough to implement them in CParserFile without modifying IParserFile. You might have to redeclare the functions as pure virtual in the interface though, but that's not something C++ normally requires.

The reason for the lack of base implementation of AddRef/Release is that you might want to do your own reference counting/memory management and destruction handling. And QueryInterface is needed to provide inheritance and backwards compability (so a IDirectDraw interface can be converted into a IDirectDraw2 interface, for example).
So pretty much the IParserFile is a placeholder of functions and the class ( CParserFile ) is what holds all the "meat" that the interface does? If this is true, why bother having interfaces then? just to allow compatibility between C and C++??

thanks for all the help thusfar doynax!!

~guyaton
Quote:Original post by guyaton
So pretty much the IParserFile is a placeholder of functions and the class ( CParserFile ) is what holds all the "meat" that the interface does? If this is true, why bother having interfaces then? just to allow compatibility between C and C++??

thanks for all the help thusfar doynax!!

Well, partially because of C compability and that you want to hide any implementation details (and thus only provide the raw interface in the header).
But also because a single interface might be implemented by many different classes (eg. IComparable) and the other way around.
Then there's the possiblity of the same function providing different behaviour in two versions of the same interface, a case that normal inheritance just doesn't handle.

In general it's the same reasons you'd use pure interfaces in any OO design plus a few technical ones.

This topic is closed to new replies.

Advertisement