Order of declaration

A

André Hänsel

Hi,

in a tutorial about COM objects, I am told to declare the following
things:

typedef HRESULT STDMETHODCALLTYPE QueryInterfacePtr(Example *, REFIID,
void **);
typedef ULONG STDMETHODCALLTYPE AddRefPtr(Example *);
typedef ULONG STDMETHODCALLTYPE ReleasePtr(Example *);

typedef struct {
QueryInterfacePtr *QueryInterface;
AddRefPtr *AddRef;
ReleasePtr *Release;
} ExampleVtbl;

typedef struct {
ExampleVtbl* lpVtbl;
DWORD count;
} Example;

In this order it doesn't work, of course. When QueryInterfacePtr is
declared, the compiler doesn't know about the Example type. When I
switch the two blocks, it's the same problem the other way around, it
doesn't know about QueryInterfacePtr.
But I cannot figure out which declarations I have to use to resolve
this.

Regards,
André
 
N

Nate Eldredge

André Hänsel said:
Hi,

in a tutorial about COM objects, I am told to declare the following
things:

typedef HRESULT STDMETHODCALLTYPE QueryInterfacePtr(Example *, REFIID,
void **);
typedef ULONG STDMETHODCALLTYPE AddRefPtr(Example *);
typedef ULONG STDMETHODCALLTYPE ReleasePtr(Example *);

typedef struct {
QueryInterfacePtr *QueryInterface;
AddRefPtr *AddRef;
ReleasePtr *Release;
} ExampleVtbl;

typedef struct {
ExampleVtbl* lpVtbl;
DWORD count;
} Example;

In this order it doesn't work, of course. When QueryInterfacePtr is
declared, the compiler doesn't know about the Example type. When I
switch the two blocks, it's the same problem the other way around, it
doesn't know about QueryInterfacePtr.
But I cannot figure out which declarations I have to use to resolve
this.

You can use a "forward declaration" of a struct type without defining
its members.

struct ExampleStruct;

and then proceed as:

typedef struct ExampleStruct Example;

typedef HRESULT STDMETHODCALLTYPE QueryInterfacePtr(Example *, REFIID, void **);
typedef ULONG STDMETHODCALLTYPE AddRefPtr(Example *);
typedef ULONG STDMETHODCALLTYPE ReleasePtr(Example *);

typedef struct {
QueryInterfacePtr *QueryInterface;
AddRefPtr *AddRef;
ReleasePtr *Release;
} ExampleVtbl;

Finally, you define `struct ExampleStruct' (aka `Example'):

struct ExampleStruct {
ExampleVtbl* lpVtbl;
DWORD count;
};
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top