2011-03-08 59 views
0

我有一个接口Interface。 我也有.h文件InterfaceFwd.h它看起来像共享指针正向声明

#ifndef Blah 
#define Blah 
#include <boost/shared_ptr.hpp> 
class Interface; 
typedef boost::shared_ptr<Interface> InterfacePtr; 
#endif 

我也有Interface.h

#ifndef SomeOtherBlah 
#define SomeOtherBlah 
class Interface 
{ 
    virtual ~Interface() 
    { 
    } 
    ... 
}; 
typedef boost::shared_ptr<Interface> InterfacePtr; 
#endif 

我是否需要担心,如果这两个文件都包含会有重复的声明InterfacePtr?在我的编译器上编译好,但标准的One-Definition Rule是否允许多个相同的typedef声明?另外,你认为我应该包括InterfaceFwd.hInterface.h,而不是重新声明InterfacePtr,或者它是好的吗?

在此先感谢

回答

2

的一个定义规则并不适用于typedef秒。 A(自己)没有定义新的变量,函数,类类型,枚举类型或模板。您明确允许重新定义以前的typedef-name以引用它已经引用的类型。

7.1.3 [dcl.typedef]:

在给定的非类范围,一个typedef说明符可以用来重新定义该范围声明的任何类型的名称来指代的类型它已经提到。