• Advertisement
Sign in to follow this  

programming interfaces

This topic is 4867 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i am trying to create an interface
[uuid(336389EB-B3BA-4278-9107-28EA51F552DA)]
interface IParserFile : IUnknown
{
	HRESULT LoadFile( [in, string]char *strFileName );
};



but it keeps having compiling errors. does anyone know a good website that explains them really well? Do they work like C++ classes with public, private, protected ? thanks!

Share this post


Link to post
Share on other sites
Advertisement
What you posted is not C++... C++ has no support for interfaces. If you want to do an interface, just create a class and make all functions pure virtual...

Oxyd

Share this post


Link to post
Share on other sites
Quote:
Original post by guyaton
i am trying to create an interface
*** Source Snippet Removed ***

but it keeps having compiling errors. does anyone know a good website that explains them really well? Do they work like C++ classes with public, private, protected ?

thanks!

Also, telling us that you have errors is not helpful in the slightest, atleast give us the errors you are getting.

Share this post


Link to post
Share on other sites
Ok, this is what I have so far:


[uuid(336389EB-B3BA-4278-9107-28EA51F552DA)]
interface IParserFile : IUnknown
{
IParserFile();
HRESULT LoadFile( [in, string]char *strFileName );
};

typedef IParserFile *LPPARSERFILE;



and here's the errors:

error C3303: 'in': attribute can only be used on 'interface methods, interface method parameters, method parameters, idl_module methods'

eerror C3303: 'string': attribute can only be used on 'members, interface methods, interface method parameters, method parameters, idl_module methods, typedefs'

thanks for all the help


Share this post


Link to post
Share on other sites
i know this is a big streach and i appriciate the help 'n support....once you create one, how do you create an instance of it, because as we all know these are not allowable:


LPPARSERFILE test = NULL;
test = new IParserFile();

IParserFile test;




Microsoft has functions that "create" them for example

LPDIRECTXFILE lpDXFile;
hr = DirectXFileCreate( &lpDXFile )



how would i create one of those?

thanks!

Share this post


Link to post
Share on other sites
Quote:
Original post by guyaton
i know this is a big streach and i appriciate the help 'n support....once you create one, how do you create an instance of it, because as we all know these are not allowable:

*** Source Snippet Removed ***

Microsoft has functions that "create" them for example
*** Source Snippet Removed ***

how would i create one of those?

thanks!
There's nothing magical about the construction of COM objects.
Just add a constructor as usual or make it private and wrap it up in an external function like microsoft does.
The reason that you might want do make it external is that the interface is generally fully virtual and any constructor is placed in the actual (and internal) class implementation.

Share this post


Link to post
Share on other sites
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 sees
class ITimer {
public:
virtual int Query() = 0;
};

// The actual implementation. This is the hidden class that does all of the work
class 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's
ITimer *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.

Share this post


Link to post
Share on other sites
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 ParserFileCreate
void ParserFileCreate( LPPARSERFILE *ppParserFile )
{
*ppParserFile = new CParserFile();
}
#endif





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


Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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!!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement