901221-此代码有什么问题? [英] 901221 - what's wrong with this code?!

查看:78
本文介绍了901221-此代码有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

struct AsVarVal
{
    bool isVar;
    byte varVal;  // 0 to 255

    const static AsVarVal Default = {false, 0};

    AsVarVal()
    {
        *this = Default;
    }
};


像枚举一样,我希望其他const静态数据成员可以在结构定义中初始化.
那么,哪个是首选? const static或static const?
thx


like enums i expect other const static data members be initializable inside struct definitions.
then, which one is preferred? const static or static const?
thx

推荐答案

这在很多方面都是错误的,我将在工作时对其进行代码审查.

直接的代码将无法编译.

通常,我写:

This is so wrong in so many way, it would be code-reviewed to oblivion where I work.

Straight code will not compile.

Usually, I write:

static const <t> var;</t>



反正
我认为您无法像在VS11 beta上那样初始化struct/classs,它将给出错误C2552:

无法使用初始化程序列表"asVarBal"初始化默认"非聚合类型.用户定义的构造函数的类型未聚合.

我什至不知道为什么要在构造函数中从静态变量中重新分配它?您是否要创建一个单例?

但我愿意接受解释...
最多



anyway,
I don''t think you can initialize struct/classes like that (on VS11 beta), it will give error C2552:

"Default" non-aggregate cannot be initialized with initializer list ''asVarBal'' Types user defined constructors are not aggregate.

I''m not even certain why you re-assigned this in the constructor from a static variable ? are you trying to create a singleton ?

but I''m open to explanation...
Max.


1>c:\visual studio\cpptest\cpptest.cpp(69): error C2059: syntax error : '{'
1>c:\visual studio\cpptest\cpptest.cpp(69): error C2334: unexpected token(s) preceding '{'; skipping apparent function body
1>c:\visual studio\cpptest\cpptest.cpp(73): error C2065: 'Default' : undeclared identifier
=


这就是我尝试时编译器抛出的内容.试图在其定义内创建结构常量显然是无效的.请记住,这与enum不同.


This is what the compiler threw up when I tried it; it''s obviously not valid to try and create a constant of your structure within its definition. And remember this is not the same as an enum.


这里有许多不符合C ++规范的东西:

1)非整数类型的变量的初始化必须在文件级别
2)使用大括号初始化器列表初始化结构要求该结构为POD(POD =普通旧数据)

要容纳1),必须将初始化移到类声明之外,而要容纳2),则必须没有构造函数.

这样就可以了
There are a numbe of things not conforming to the C++ specifications, here:

1) The initialization of a variable of non integral type must be at file level
2) initializing a struct with a brace initilializer list requires the struct to be a POD (POD = Plain Old Data)

To accommodate 1) you must move the initialization outside of the class declaration and to accommodate 2) you must have no constructors.

In this way it works
struct AsVarVal
{
    bool isVar;
    unsigned char varVal;  // 0 to 255

    const static AsVarVal Default;
};

const AsVarVal AsVarVal::Default = {false, 0};



如果您想拥有构造函数,则可以采用其他方法,例如




If you want to have constructors, you may have a different approach, like this


struct AsVarVal
{
    bool isVar;
    unsigned char varVal;  // 0 to 255

    AsVarVal() :isVar(false), varVal(0)
    {}

    AsVarVal(bool isVar_, unsigned char varVal_)
        :isVar(isVar_), varVal(varVal_)
    {}
};





大约1)我的意思是符合语言规范".
我们没有人写过任何东西.他们只是是".

大约2)是的,您做对了.关于为什么",这与说可以重复声明(因为它们不实例化任何东西,只为编译器定义符号)而重复的规则完全相同,而定义则不能重复.
为什么存在此规则,以及C ++为什么将标头用作粘贴"(而不是像其他语言一样用作符号目录")……这就是C ++规范的方式. 为什么"是C ++必须与C向下兼容,而C又是高级汇编程序".这就是C的工作方式.

请注意,您所谓的类定义"(在第4章中)实际上是一个声明":它没有实例化任何对象,它只是告诉该类如何构造,但没有为其分配任何空间. .它只是说需要多少.
但是初始化的静态变量是一个定义,因为它需要为其保留一些空间.

积分常量(在这种情况下)是可以容忍的一般规则的一个例外,仅因为积分是由编译器本身求值的(实际上,积分常量未分配为对象:它只是一个数字,它将替换该常量)符号在使用的每个位置).出现此异常的原因在于这样一个事实,即可以在大多数处理器上的单个机器代码指令中直接转换以整数作为参数的运算.其他类型的常量不适合单个指令,需要引用或数据移动(因此需要在数据存储器中使用空格")

大约3)术语普通旧数据"(缩写为POD)是C ++语言规范所称的符合C ++所基于的C子集规范的结构".本质上是C结构(没有构造函数,析构函数,访问说明符,虚拟方法等)

大约4)我可以理解您的感受(C ++不是一种简单的语言...),但是-技术上讲-我的解决方案不是解决方法:它们不符合语言规范的要求.

通过查看代码,似乎您正在使用C ++,因为它是Java或C#.
C ++是一种完全不同的语言,具有完全不同的理念.这导致了完全不同的编码样式和模式实现.





About 1) I meant "conformant to the language specification".
Something none of us wrote. They simply "are".

About 2) Yes, you got it right. About the "why", this is exactly the same rule that says that declarations can be repeated (since they don''t instantiate anything, just define symbols for the compiler) but definition cannot.
Why this rule exist, and why C++ use headers as "paste in" (not as "symbols catalogs", like other languages do) ... that''s how C++ specification are. The "why" is that C++ must be backward compatible with C that is -in turn- an "high level assembler". And that''s how C works.

Note that what you call "class definition" (in 4) ) is in fact a "declaration": it doesn''t instantiate any object, it just tells how the class is structured, but doesn''t allocate any space for it. It just says how much of it is required.
But an initialized static variable is a definition, since it requires some space to be reserved for it.

Integral constants -in this context- are an exception to that general rule that can be tolerated just because integrals are evaluated by the compiler itself (in fact an integral constant is not allocated as an object: it is just a number that will replace the constant symbol in every place it is used). The reason of this exception resides in the fact that operation taking an integer as a parameter can be translated directly in a single machine code instruction on the most of the processors. Other type of constants don''t fit a single instruction and require a reference or a data move (and hence "space" in the data memory)

About 3) The term "Plain Old Data" (abbreviated as POD) is what the C++ language specs calls "a struct that conforms to the C subset specs C++ is based on". Essentially C struct (with no constructor, destructor, access specifiers, virtual methods etc.)

About 4) I can understand your feeling (C++ is not a simple language...), but -technically speacking- my solutions aren''t workarounds: they are nothign more that what the language specifications require.

By looking at your code, it seems you are using C++ as it is Java or C#.
C++ is a completely different language with a completely different philosophy. That leads to a completely different coding style and patterns implementation.


这篇关于901221-此代码有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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