별도의 헤더가있는 두 클래스가 있습니다 (class Renderer
및 class Texture
). Texture
인스턴스는 Renderer
의 메모리 풀에있는 일부 데이터를 관리하기 위해 설계 되었기 때문에 Texture
인스턴스는 Renderer
인스턴스와 독립적으로 존재할 수 없습니다. Texture
인스턴스에 대한 핸들을 얻으려면 Renderer::loadTexture
을 올바른 파일 이름으로 호출하고 Renderer
은 파일 이름이 일치하는 Texture
개체 데이터베이스를 검색하고 일치하는 항목이없는 경우 새 텍스처를 인스턴스화합니다.클래스 델라clation의 순환 종속성
//renderer.h
#include "texture.h"
class renderer
{
public:
Texture* loadTexture(std::string fileName);
private:
std::map<std::string, Texture*> textureDb;
};
//texture.h
class Texture
{
public:
Texture(std::string imageFileName);
};
는 class Texture
개인의 생성자를 만들기 위해 논리, 그리고 class Renderer
의 친구로 Texture * Renderer::loadTexture(std::string filename)
를 선언 할 것이라고 나에게 발생했습니다 texture.h 경우
//renderer.h
#include "texture.h"
class renderer
{
public:
Texture* loadTexture(std::string fileName);
private:
std::map<std::string, Texture*> textureDb;
};
//texture.h
class texture;
#include "renderer.h"
class Texture
{
private:
Texture(std::string imageFileName);
Texture(const Texture &);
friend Texture* loadTexture(std::string);
};
그러나 이것은 단지 컴파일 class Texture
에는 class Renderer
이 이미 정의되어 있어야하므로 renderer.h를 포함하기 전에 항상 포함됩니다. 이것을 막는 가장 좋은 방법은 무엇입니까? 두 경비원 모두 포함 된 경비원이지만 간결함을 위해 여기서는 생략되었습니다.
는 "당신은 약간의 설계를 변경할 수있는 경우를 참조하십시오." Texture는 클래스 렌더러에 의존하지만 다른 방법은 아닙니다. 모든 소유 관계와 데이터로드를 자동으로 처리하는 리소스 관리 시스템을 작성했습니다. – jms