问题标签 [in-class-initialization]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
c++ - C++11 中是否包含“类中成员初始化”功能?
在类初始化特性中,它允许在类本身内部初始化普通成员,
这在最新的编译器 gcc-4.6 (with -std=c++0x
) 中给出了错误。此功能是否已纳入 C++11 标准或 gcc 仍然不支持它?
c++ - 使用 C++,我在创建类时得到指针 0xcdcdcdcd - 发生了什么?
- - - - - - - - 编辑 - - - - - - - - - - - -
我抓住了错误的值进行比较, cdcdcdcd 来自其他地方。我仍然有我的方法在达到异常之前抛出异常,但我的问题出在其他地方,我希望有一种方法可以“取消发布”我原来的问题。谢谢您的帮助。
- - - - - - - - 编辑 - - - - - - - - - - - -
我有一个类(MyClass),我从某个第三方类继承(它又从直接显示类派生 - 如果这很重要,则来自 CBaseFilter)。我写了这段代码:
尝试进行方法调用会导致抛出异常。经过一番调查,我发现当我取消引用 val(val 本身似乎是有效的......像 0x0777fc90 之类的东西)时,它包含 0xcdcdcdcd。我环顾四周,似乎这可能表明内存已在堆上分配,但未初始化。
这意味着什么?!对 new 的调用如何成功(val != NULL),但内存还没有初始化到足以有一个指向它的指针?某些基类初始化是否可能出现问题?如果是这样 - 我在寻找什么?
c++ - 初始化列表中的 try / catch 是如何工作的?
我们认为可能会发生初始化异常。所以我们写了 try/catch 块。
但是 catch 在更深一层上重新抛出异常。这意味着下一个代码
将输出:
为什么这个 try / catch 块的行为与普通的 try / catch 块不同?
完整代码示例:http: //ideone.com/XjY2d
java - 初始化 bean 中的字段的顺序
我有一个这样的豆子:
但是当 Spring 实例化 bean 时,它会抛出一个NullPointerException
. 所以我想知道该字段two
是否在 field 之前初始化one
,导致 NPE。谁能告诉我在 bean 中初始化字段的顺序是什么?
c++ - 位域“类内初始化”导致“错误:需要左值作为赋值的左操作数”
使用 C++11“类内初始化”功能初始化位域的正确语法是什么?
c++ - Why can't I make in-class initialized `const const std::string` a static member
I have the following working code:
Is there a good reason why it is not possible to make the test a static const
? I do understand prior to c++11 it was constrained by the standard. I thought that c++11 introduced in-class initializations to make it a little bit friendlier. I also not such semantic are available for integral type since quite some time.
Of course it works with the out-of class initialization in form of const std::string A::test = "42";
I guess that, if you can make it non-static, then the problem lies in one of the two. Initializing it out-of-class scope (normally const
s are created during the instantiation of the object). But I do not think this is the problem if you are creating an object independant of any other members of the class. The second is having multiple definitions for the static member. E.g. if it were included in several .cpp
files, landing into several object-files, and then the linker would have troubles when linking those object together (e.g. into one executable), as they would contain copies of the same symbol. To my understanding, this is exactly equal to the situation when ones provides the out-of-class right under the class declaration in the header, and then includes this common header in more than one place. As I recall, this leads to linker errors.
However, now the responsibility of handling this is moved onto user/programmer. If one wants to have a library with a static
they need to provide a out-of-class definition, compile it into a separate object file, and then link all other object to this one, therefore having only one copy of the binary definition of the symbol.
I read the answers in Do we still need to separately define static members, even if they are initialised inside the class definition? and Why can't I initialize non-const static member or static array in class?.
I still would like to know:
- Is it only a standard thing, or there is deeper reasoning behind it?
- Can this be worked-around with the
constexpr
and user-defined literals mechanisms. Both clang and g++ say the variable cannot have non-literal type. Maybe I can make one. (Maybe for some reason its also a bad idea) - Is it really such a big issue for linker to include only one copy of
the symbol? Since it is
static const
all should be binary-exact immutable copies.
Plese also comment if I am missing or missunderstanding something.
c++ - 用户声明的默认构造函数 + 类内初始化器!= 用户提供的构造函数?
Clang 文档巧妙地解释了
如果一个类或结构没有用户定义的默认构造函数,C++ 不允许你像这样默认构造它的 const 实例([dcl.init],p9)
基本原理是,如果 const 对象未正确初始化,则以后无法更改。下面的代码只有一个用户声明的默认构造函数Test
,但它的所有成员都有类内初始化器,
所以用户提供默认构造函数的理由对我来说似乎是多余的。事实上,g++ 4.8.1 确实可以毫无问题地编译它(在线示例),尽管Clang <= 3.2没有。
问题:为什么完整的类内初始化器 + 用户声明的默认构造函数的组合不足以默认构造一个 const 对象?C++14 标准是否正在进行修复?
更新:任何人都可以尝试使用 Clang 3.3 / 3.4 来查看与 Clang 3.2 相比是否已修复此问题?
c++ - C++11“类内初始化”功能不适用于联合
最小代码示例:
现在,如果我们声明 aB obj;
那么将obj.u.i
分配一个垃圾值而不是100
. 在此处查看演示。(垃圾值因优化标志等而异)。
“类内初始化”功能是否应该与联合一起使用。
- 如果是,那么正确的语法是什么?或者这是一个 g++ 错误?
- 如果没有,那该怎么
int i = 100;
办?
c++ - 为什么在初始化成员变量后需要另一组大括号?
我试图使用大括号初始化(谢天谢地,Visual Studio 2013 实际支持),但由于某种原因,当我在课堂上这样做时,它需要两组大括号。例如:
为什么需要我说number { 5 }{}
?这对我来说真的没有视觉意义。
c++ - 为什么静态成员的类内初始化会违反 ODR?
Stack Overflow 上有几个问题,类似于“为什么我不能在 C++ 中初始化静态数据成员”。大多数答案都引用标准告诉您可以做什么;那些试图回答为什么通常指向一个链接(现在看似不可用)的人 [编辑:实际上它是可用的,见下文] 在 Stroustrup 的网站上,他指出允许静态成员的类内初始化将违反单一定义规则(ODR )。
然而,这些答案似乎过于简单。编译器完全能够在需要时解决 ODR 问题。例如,考虑 C++ 标头中的以下内容:
如果我在多个翻译单元中进行实例化,编译器/链接器的魔法就会发挥作用,并且我会在最终的可执行文件中TemplateExample<0>
获得一份副本。TemplateExample<0>::str
所以我的问题是,考虑到编译器显然可以解决模板类的静态成员的 ODR 问题,为什么它不能对非模板类也这样做呢?
编辑:Stroustrup 常见问题解答可在此处获得。相关语句是:
但是,为了避免复杂的链接器规则,C++ 要求每个对象都有唯一的定义。如果 C++ 允许对需要作为对象存储在内存中的实体进行类内定义,则该规则将被打破
然而,那些“复杂的链接器规则”似乎确实存在并且在模板案例中使用,那么为什么不在简单案例中呢?