Jump to content
  • Advertisement
Sign in to follow this  
eben

Some newbie COM/C++ questions.

This topic is 4831 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 have some questions about a piece of code in this book I'm reading. The section I'm on is just beginning to talk about COM objects and this is a demo of the concept. The full source is here if you want to see it: http://eben.50megs.com/demo5_1.cpp 1:A single pointer being cast as a "pointer to a pointer"
// create two NULL pointers the the IX and IY interfaces
IX *pix=NULL;
IY *piy=NULL;

// from the original COM object query for interface IX
punknown->QueryInterface(IID_IX, (void **)&pix);

How can a single pointer *pix be cast to a void** pointer(a pointer to a pointer)? 2: Why are these functions virtual?
// INTERFACES ////////////////////////////////////////////////////////////////////////////////

// define the IX interface
interface IX: IUnknown
{

virtual void __stdcall fx(void)=0;

}; 

// define the IY interface
interface IY: IUnknown
{

virtual void __stdcall fy(void)=0;

}; 


// CLASSES AND COMPONENTS ///////////////////////////////////////////////////////////////////

// define the COM object
class CCOM_OBJECT :	public IX,
                    public IY
{
public:

	CCOM_OBJECT() : ref_count(0) {}
	~CCOM_OBJECT() {}

private:

virtual HRESULT __stdcall QueryInterface(const IID &iid, void **iface);
virtual ULONG __stdcall AddRef();
virtual ULONG __stdcall Release();

virtual	void __stdcall fx(void) {cout << "Function fx has been called." << endl; }
virtual void __stdcall fy(void) {cout << "Function fy has been called." << endl; }

int ref_count;

};

I understand why the functions in the interfaces are pure virtual, but why are they virtual in the CCOM_OBJECT class? Why not just define them as ordinary functions as specified by the signatures given in the interfaces? And also, what's up with the "interface IX:IUnknown {... };" syntax. I've never seen it before. When I make interfaces I just declare a class and make all the functions virtual. Is it better to use this convention or the one in this source file? Thanks.

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by eben

How can a single pointer *pix be cast to a void** pointer(a pointer to a pointer)?

px is a IX pointer. &px is a pointer to an IX pointer, which can be cast to a pointer to a void pointer.

Quote:

2: Why are these functions virtual?
*** Source Snippet Removed ***
I understand why the functions in the interfaces are pure virtual, but why are they virtual in the CCOM_OBJECT class? Why not just define them as ordinary functions as specified by the signatures given in the interfaces?

Just because the author prefers to keep the virtual in the function signatures. Even if you don't declare them virtual in the derived class, if it was virtual in the base class, it's virtual in the derived class.

Quote:

And also, what's up with the "interface IX:IUnknown {... };" syntax. I've never seen it before. When I make interfaces I just declare a class and make all the functions virtual. Is it better to use this convention or the one in this source file?

interface is a #define for struct. This is a struct that inherits from the IUknown interface. You have to inherit from IUknown in COM in order for your class to be a COM class. Otherwise it doesn't properly have the vtable for the AddRef(), Release() and QueryInterface() functions.

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!