2013-08-18 1 views
0

간단한 버전 :전체 형이 아직 알려지지 않은 형식을 가질 때 typedef를 전달 선언 할 수 있습니까? 문제의

library.h

typedef library::whatever::something::complicated X; 

나는 library.h를 포함하지 않고 X를 앞으로 선언 할 수 있습니까?

// forward declare X 

void foo (const X &); 

Bar.h :

#include <library> 
typedef library::type<blah>::whatever Bar; 
typedef Bar X; 

foo.cpp의 :

Foo.h :


은 기본적으로 제가하고 싶은 것은 이것이다

#include <Foo.h> 
#include <Bar.h> 

foo (const X &) {... 
,

MAIN.CPP는 :

#include <Foo.h> 
#include <Bar.h> 

foo (X()); 

전제는 내가 필요하지 않는 <library>을 포함하고 싶지 않는다는 것입니다. 그럼에도 불구하고 다른 헤더와 심하게 상호 작용한다고 가정 해보십시오. 그럼에도 불구하고이 다른 헤더는 foo이라는 선언을 원합니다. 라이브러리를 포함시키지 않고 foo()을 호출 할 수는 없지만 .cpp가 걱정할 필요가 있습니다.

X를 일반 클래스 C에 typedef 지정하려면 C를 전달 선언하는 것으로 충분하지만 여기서는 작동하지 않습니다.

X를 typedef로 전달 선언 할 수 있지만 이미 정의가 library::type<blah> 인 경우에만 AFAIK를 사용할 수 있습니다 (어쩌면 blah 정의도 가능).

전달 선언의 전체 지점은 이러한 유형의 정의가 #include이 아닙니다. 이 경우

namespace std 
{ 
    template <typename T> 
    class vector; 
} 

typedef std :: vector <int> Buffer; 

void foo (const Buffer &); 

#include <vector> // ok until here 

int main() 
{ 
    foo (Buffer()); 
} 

STD의 전방 선언 : 벡터의 정의와 호환이다 : 여기

는 관련된 문제이다. 내가 정말로 인데, #include <vector> (또는 그것이 어떤 외부 클래스 이건간에)을 원하지 않는 척하자.

이 방법이 있습니까?

답변

0

중첩 클래스 선언을 전달할 수 없습니다 (this answer 참조).클래스가 중첩되지 않는 경우

그러나, 당신이 이런 식으로 작업을 수행 할 수 있습니다

// inside library_fwd.hpp: 
namespace library { 
namespace whatever { 

template <class T> 
class Something; 
typedef Something<int> IntSomething; 

} // namespace whatever 
} // namespace library 

과 : 표준 : : 벡터의 예와

// inside foo.cpp 
#include "library_fwd.hpp" 
void foo(const library::whatever::IntSomething&); 

귀하의 문제는 충돌에 관한 것입니다 선언. std::vector을 참조하십시오. 수정 :

namespace std { 
    template <class T> 
    class allocator; 

    template <typename T, class Allocator> 
    class vector; 
} 

typedef std::vector<int, std::allocator<int>> Buffer; 

void foo(const Buffer &); 

#include <vector> // now ok 

int main() { 
    foo (Buffer()); 
} 

PS 일부 성병 LIB 순방향 선언 예 : http://en.cppreference.com/w/cpp/header/iosfwd

1

typedef library::type<blah>::whatever Bar;은 class library :: type의 전체 정의가 필요하기 때문에 그렇게하기가 어렵다고 생각합니다. 해결할 수있는 forward-declare가 아닙니다.

관련 문제