重新定义允许在C,但不是在C ++? [英] Redefinition allowed in C but not in C++?

查看:127
本文介绍了重新定义允许在C,但不是在C ++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么在C,但不是在C ++中,这code的工作?

  INT I = 5;
INT I; //但如果我写INT I = 5;我再次用C得到错误也诠释主(){  使用我//
}


解决方案

<一个href=\"http://publib.boulder.ibm.com/infocenter/comphelp/v7v91/index.jsp?topic=/com.ibm.vacpp7a.doc/language/ref/clrc03tentative_defn.htm\">Tentative定义允许在C,但不是在C ++

一个初步的定义是没有存储类说明并没有初始化任何外部数据声明。

C99 6.9.2 / 2


  

这是鉴定网络连接器为具有网络连接文件范围的对象的声明没有初始化,然后
  无存储类特定连接器或与存储类特定连接器的静态,构成了
  暂定德网络nition。如果翻译单元包含一个一个或多个暂定德科幻nitions
  鉴定连接器和翻译单元包含该鉴定连接器无需外接音响去定义,然后
  行为是完全一样的,如果翻译单元包含了一个文件作用域声明
  鉴定连接器,与该复合类型的翻译单元的结束,有一个初始化
  等于0


所以 INT I 是一个试探性的定义。 C编译器将所有暂定定义组合成一个单一的定义 I

在C ++中的code是形成不良,由于 一个定义规则 (第3.2 / 1 ISO C ++)


  

没有翻译单元的应包含任何变量,函数,类型,枚举类型或模板的多个定义。




  

//但如果我写 INT I = 5; 我再次得到错误的C也


由于在这种情况下它不再保留,因为初始的暂定定义(5)。


只为信息起见


  

J.5.11多重外部定义


  
  

有可能是一个对象的识别符,有或没有明确使用关键字的extern的多于一个外部定义;如果定义不同意,或一个以上的初始化,这种行为是未定义(6.9.2)。


另外,请查阅该<一个href=\"http://stackoverflow.com/questions/1433204/what-are-extern-variables-in-c/1433387#1433387\">excellent张贴在外部变量的。

Why does this code work in C but not in C++?

int i = 5;
int i; // but if I write int i = 5; again I get error in C also

int main(){

  // using i
}

解决方案

Tentative definition is allowed in C but not in C++.

A tentative definition is any external data declaration that has no storage class specifier and no initializer.

C99 6.9.2/2

A declaration of an identifier for an object that has file scope without an initializer, and without a storage-class specifier or with the storage-class specifier static, constitutes a tentative definition. If a translation unit contains one or more tentative definitions for an identifier, and the translation unit contains no external definition for that identifier, then the behavior is exactly as if the translation unit contains a file scope declaration of that identifier, with the composite type as of the end of the translation unit, with an initializer equal to 0.

So int i is a tentative definition. The C compiler will combine all of the tentative definitions into a single definition of i.

In C++ your code is ill-formed due to the One Definition Rule (Section 3.2/1 ISO C++)

No translation unit shall contain more than one definition of any variable, function, class type, enumeration type or template.


// but if I write int i = 5; again I get error in C also

Because in that case it no longer remains a tentative definition because of the initializer (5).


Just for the sake of information

J.5.11 Multiple external definitions

There may be more than one external definition for the identifier of an object, with or without the explicit use of the keyword extern; if the definitions disagree, or more than one is initialized, the behavior is undefined (6.9.2).

Also check out this excellent post on external variables.

这篇关于重新定义允许在C,但不是在C ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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