共享指针前向声明
我有一个接口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 的重复声明?在我的编译器上,这编译得很好,但是标准的单定义规则是否允许多个相同的 typedef 声明?另外,您认为我应该将 InterfaceFwd.h
包含到 Interface.h
中,而不是重新声明 InterfacePtr
还是就这样?
提前致谢
I have an interface Interface
.
I also have a .h file InterfaceFwd.h
which looks something like
#ifndef Blah
#define Blah
#include <boost/shared_ptr.hpp>
class Interface;
typedef boost::shared_ptr<Interface> InterfacePtr;
#endif
I also have Interface.h
#ifndef SomeOtherBlah
#define SomeOtherBlah
class Interface
{
virtual ~Interface()
{
}
...
};
typedef boost::shared_ptr<Interface> InterfacePtr;
#endif
Do I need to worry that if both files are included there will be duplicate declaration of InterfacePtr? On my compiler this compiles fine, but does the standard One-Definition Rule allow multiple identical typedef-declarations? Also, do you think I should include InterfaceFwd.h
into Interface.h
instead of redeclaring InterfacePtr
or it's fine as it is?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
单一定义规则不适用于
typedef
。typedef
(单独)不定义新的变量、函数、类类型、枚举类型或模板。明确允许您重新定义先前的typedef-name以引用它已经引用的类型。7.1.3 [dcl.typedef]:
The one definition rule doesn't apply to
typedef
s. Atypedef
(on its own) doesn't define a new variable, function, class type, enumeration type or template. You are explicitly allowed to redefine a previous typedef-name to refer to the type that it already refers to.7.1.3 [dcl.typedef]: