Published 2020. 10. 27. 21:36

 What the designers of COM discovered was virtually all of the C++ compilers on the Windows platform happened to implement virtual functions in the same way. This was key discovery. They then decided to take this coincidence as a blessing and use it as the basis for COM's interface inheritance, which enables polymophism and COM's version of object-oriented programming. COM also expects methods to implement the standard calling convention. 

 

1. Library.h Library.cpp App.cpp Library.def 를 수정한다.

//Library.h
#pragma once

struct IHen 
{
	virtual void __stdcall Cluck() = 0;
	virtual void __stdcall Roost() = 0;
    /*
    virutal void __stdcall Delete() = 0;
    */
};

IHen* __stdcall CreateHen();
//Library.cpp
#include "Library.h"
#include <Windows.h>

#define TRACE OutputDebugString

struct Hen : IHen 
{
	Hen() 
	{
		TRACE("Constructor\n");
	}

	~Hen() 
	{
		TRACE("Destructor\n");
	}

	void __stdcall Cluck() 
	{
		TRACE("Cluck");
	}

	void __stdcall Roost() 
	{
		TRACE("Roost");
	}
    /*
    void __stdcall Delete()
    {
        delete this;
    }
    */
};

IHen* __stdcall CreateHen() 
{
	return new Hen;
}
//Library.def
EXPORTS

CreateHen
//App.cpp
#include "Library.h"

int main()
{
	IHen* hen = CreateHen();

	hen->Cluck();
	hen->Roost();
	delete hen;
    //hen->Delete();
}

2. IHen 인터페이스는 pure virtual 함수로만 이루어져 있고, 이것들은 다른 C-chain 앱에서 사용할 수 있다. 그러나 생성자와 소멸자는 C++에서만 된다.

 

3. Dynamaic linking을 사용하면 컴파일러는 함수의 내용에 자체에 관심이 없다. new 를 사용했지만 CreateHen 메소드를 통해서 안전하게 캡슐화가 이루어졌기 때문에, 다른 생성자는 C-Chain 앱에서 사용할 수 있다.

 

4. App.cpp에서는 생성자는 호출이 되지만, delete를 했음에도 소멸자는 호출되지 않는다.

 

5. 생성자와 마찬가지로 wrapper 펑션에서 delete를 호출하면 된다. 이때, wrapper 역시 pure virtual 로 하고, 구현은 Hen에서 한다. 주석을 보거라

'삽질 > COM' 카테고리의 다른 글

COM study note 6 인터페이스 상속  (0) 2020.10.28
COM study note 5 레퍼런스 카운터  (0) 2020.10.28
COM study note 3 다이나믹 링킹  (0) 2020.10.27
COM study note 2 역사  (0) 2020.10.26
COM study note 1 소개  (0) 2020.10.26
복사했습니다!