小编M.G*_*.G.的帖子

初始化C/C++中的结构的const成员...依赖于编译器?

最近我使用Borland C++ 5.2在遗留环境中遇到了编译器错误.我有一个.cpp文件,其中包含一些我无法控制的C源头.头部包含一个包含const成员的结构定义,并且编译器抱怨"没有构造函数的类中的常量成员".经过调查,此错误似乎与编译器有关.以下是来自各种编译器的一些示例代码:

#include <stdio.h>

typedef struct {
   const float a;
} _floater;

int main()
{
   _floater f = {5.1F};

   printf("%f\r\n",f.a);

   return 0;
}
Run Code Online (Sandbox Code Playgroud)

Borland 5.2

E:\Projects\Scratchpad>bcc32 -P const_float.c
Borland C++ 5.2 for Win32 Copyright (c) 1993, 1997 Borland International
const_float.c:
Error const_float.c 13: Constant member ' ::a' in class without constructors
*** 1 errors in Compile ***
Run Code Online (Sandbox Code Playgroud)

Microsoft VS 2003 .NET:

E:\Projects\Scratchpad>cl /TP const_float.c
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8804 for 80x86
Copyright (C) Microsoft Corp 1984-1998. …
Run Code Online (Sandbox Code Playgroud)

c++

5
推荐指数
1
解决办法
3017
查看次数

标签 统计

c++ ×1