Ock*_*ham 4 c++ class header-files
可能重复:
c ++中的前向声明是什么?
我只是对这段代码在这个简单的例子中做了什么有疑问.我查找了朋友类并了解它们是如何工作的,但我不明白顶层的类声明实际上在做什么(即机器人).这是否意味着Happy类可以使用Robot对象,但是它们无法访问其私有部分,任何信息都将被欣赏.
#include <stdlib.h>
#include <stdexcept>
template <typename T> // What is this called when included
class Robot; // is there a special name for defining a class in this way
template <typename T>
class Happy
{
friend class Joe<T>;
friend class Robot<Happy<T> >;
// ...
};
Run Code Online (Sandbox Code Playgroud)
这是一个前瞻性声明.
它只是告诉编译器,Robot稍后将定义一个名为的模板,并且它应该期望该定义.
在此期间(即,直到Robot正式定义),它允许程序员在代码中引用该类型.在你给的例子中,有必要声明Robot作为friend对的Happy类.(谁选了这些名字?!)
这是避免循环依赖并最小化编译时间/开销的常见策略.