IInterfaceSupports (C++)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This example illustrates the use of the IInterface.Supports method.

Diagram

The following entities are defined:

  • Two interfaces—I1 and I2.
  • One class that implements both I1 and I2TVeryImportantClass.
  • I1, I2, and TVeryImportantClass instances.

The I1 instance is obtained by casting the TVeryImportantClass instance to I1. The I2 instance is obtained by calling the IInterface.Supports method on the I1 instance.

The TCppInterfacedObject class is used to avoid the implementation of the QueryInterface, _AddRef, and _Release methods here.

Code

#include <System.hpp>

#include <iostream>
using namespace std;

#include <tchar.h>
// ---------------------------------------------------------------------------

// Interface 1
__interface __declspec(uuid("{FC0A2D2A-8483-4A39-B55C-804EE2D87F05}")) I1
		: public IInterface {
public:
	virtual void __fastcall f1() = 0;
};
typedef System::DelphiInterface<I1>_di_I1;

// Interface 2
__interface __declspec(uuid("{B3FE7C6C-FBB3-4340-BB0F-F1408C577E0B}")) I2
		: public IInterface {
public:
	virtual void __fastcall f2() = 0;
};
typedef System::DelphiInterface<I2>_di_I2;

// Class
class TVeryImportantClass : public TCppInterfacedObject<I1, I2> {

public:
	void __fastcall f1() {
		cout << "f1" << endl;
	}

	void __fastcall f2() {
		cout << "f2" << endl;
	}
};

#pragma argsused

int _tmain(int argc, _TCHAR* argv[]) {
	_di_I1 i1;
	_di_I2 i2;

	i1 = static_cast<I1*>(new TVeryImportantClass());

	if (i1->Supports(i2)) {
		i2->f2();
	}
	else {
		// Should not happen.
		cout << "Interface unsupported" << endl;
	}

	return 0;
}

Console Output

f2

Uses

Personal tools
Assistance