foo.h 파일
#ifndef __FOO_H
#define __FOO_H
class Goo; // 아래 Goo* goo 사용(컴파일시 오류를 없애기 위해) 했는데 이렇게 하는게 맞는지 모르겠습니다. ;;
// 이것의 정확한 의미도 알고 싶구요. TC++PL에서 본것 같은데 책 내용이 많아서 어느 부분에서 봤는지 모르겠습니다.
class Foo
{
public:
Foo() {};
~Foo() {};
private:
Goo* goo;
char str[10];
};
#include "Goo.h" // include를 아래서 하는 이유???
// 위에서 하면 foo.h 내용은 포함이 되지 않는데 그 이유 말고 다른 이유가 존재 합니까?
#endif
Goo.h 파일
#ifndef __GOO_H
#define __GOO_H
#include "Foo.h"
class Goo
{
public:
Goo() {};
~Goo() {};
private:
Foo* foo;
char str[10];
};
#endif
test.cpp
#include "Foo.h"
void main()
{
Foo fo;
Goo go;
}
ifndef-define-endif 블록으로 싸놓지 않으면 무한 상호참조가 돼서 아비규환이 되고,
블록으로 싸놓은 경우 뒤에 해석된 헤더파일 한쪽은 내용이 없는 상태가 되어버립니다.
class Foo; 같이 클래스 이름만 전방선언해두고 포인터를 사용하는 건, 포인터는 어떤 형의 포인터이건 간에 CPU의 처리 비트수에 따라가는 용량을 사용하기 때문에 (32비트 컴퓨터이니 32비트 -> 4바이트)
이름만 대충 맞춰두면 실제 어떤형이건간에 끼워맞추는데는 지장이 없기 때문입니다.