嵌套结构C和C ++ [英] Nested structures in C and C++

查看:117
本文介绍了嵌套结构C和C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有背后允许C中的结构之外,但不是在C嵌套结构的知名度任何意义++?我没有发现任何参考或相关性。

 一个结构
{
  结构B {
  };
};诠释主(){
  结构b VAR; //允许使用C不是C ++。
}


解决方案

这是在C有效的,因为C有在所有非本地类型(即函数不声明类型)定义一个命名空间;没有使用名称空间或嵌套类型的作用域。

在C ++中,键入 B 嵌套作为类的成员 A ,所以它的名字必须用合格范围在其声明。

Is there any significance behind allowing the visibility of nested structures outside the structure in C but not in C++? I did not find any reference or relevance.

struct a
{
  struct b{
  };
};

int main(){
  struct b var;  // allowed in C not in C++.
}

解决方案

It is valid in C because C has a single namespace in which all nonlocal types (i.e., types not declared in functions) are defined; there is no scoping of types using namespaces or nesting.

In C++, type b is nested as a member of class a, so its name must be qualified with the scope in which it is declared.

这篇关于嵌套结构C和C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆