您的位置:首页 > 编程语言 > C语言/C++

c++前向声明

2016-05-19 13:46 232 查看

前向声明的几种方式

前向声明

class A:
A f();

详细类型声明

class A f();

两种方式的区别

前向声明

struct s { int a; };
struct s; // does nothing (s already defined in this scope)
void g() {
struct s; // forward declaration of a new, local struct "s"
// this hides global struct s until the end of this block
s* p;     // pointer to local struct s
struct s { char* p; }; // definitions of the local struct s
}


详细类型说明

class U;
namespace ns{
class Y f(class T p); // declares function ns::f and declares ns::T and ns::Y
class U f(); // U refers to ::U
Y* p; T* q; // can use pointers and references to T and Y
}

详细类型说明,只有在查找不到对应名字的同类型的东西,才会引入前向声明的效果。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: