in-class-initialization

Order in which fields in a bean are initialized

╄→гoц情女王★ 提交于 2020-01-14 06:16:18
问题 I have a bean like this: @Component @DependsOn("SomeType") Class A{ @Autowired SomeType one; String two = one.someMethod(); int three; } In my application context xml, I have: <bean id="two" class="a.b.c.SomeType"></bean> <context:component-scan base-package="a.b.c"/> <context:annotation-config/> But while Spring instantiates the bean, it throws a NullPointerException . So I'm wondering if the field two is initialized before field one , causing the NPE. Can anyone tell me in which order

Why can't I make in-class initialized `const const std::string` a static member

我是研究僧i 提交于 2019-12-30 18:07:51
问题 I have the following working code: #include <string> #include <iostream> class A { public: const std::string test = "42"; //static const std::string test = "42"; // fails }; int main(void){ A a; std::cout << a.test << '\n'; } 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

Unique pointer in-class initialization

浪尽此生 提交于 2019-12-19 08:11:21
问题 Suppose I have a unique_ptr member object that I want to initialize in-class, see the code below. Why do I have to use uniform initialization (curly braces)? The second declaration spits an error, something like so.cpp:10:31: error: expected parameter declarator std::unique_ptr<Foo> upf2(new Foo); ^ so.cpp:10:31: error: expected ')' so.cpp:10:30: note: to match this '(' std::unique_ptr<Foo> upf2(new Foo); ^ 2 errors generated. And I don't think is a most vexing parse issue, at least I don't

Class initialization failing

醉酒当歌 提交于 2019-12-12 05:52:26
问题 # Class and Instance Variables class Dog: kind = 'canine' def __int__(self, name): self.name = name self.tricks = [] d = Dog('Fido') e = Dog('Buddy') print(d.kind) print(e.kind) print(d.name) print(e.name) Error report: Traceback (most recent call last): File "dog.py", line 13, in <module> d = Dog('Fido') TypeError: object() takes no parameters 回答1: You have a typo. The method __int__ should be called __init__ . 来源: https://stackoverflow.com/questions/44611351/class-initialization-failing

With C++, I get pointer with 0xcdcdcdcd when creating a class - what is happening? [duplicate]

守給你的承諾、 提交于 2019-12-06 11:58:49
问题 This question already has answers here : When and why will an OS initialise memory to 0xCD, 0xDD, etc. on malloc/free/new/delete? (9 answers) Closed 2 years ago . ----------------EDIT----------------------- I was grabbing the wrong value for comparison, the cdcdcdcd was coming from somewhere else. I still have my methods throwing exceptions before they are even reached, but my problem lies elsewhere, I wish there was a way to just "unpost" my original question. Thanks for the help. ----------

User-declared default constructor + in-class initializers != user-provided constructor? [duplicate]

时光毁灭记忆、已成空白 提交于 2019-12-05 13:01:34
问题 This question already has answers here : Why does C++ require a user-provided default constructor to default-construct a const object? (5 answers) Closed 2 years ago . The Clang documentation neatly explains that If a class or struct has no user-defined default constructor, C++ doesn't allow you to default construct a const instance of it like this ([dcl.init], p9) The rationale being that if a const object is not correctly initialized, it cannot be changed later on. The following code has

With C++, I get pointer with 0xcdcdcdcd when creating a class - what is happening? [duplicate]

微笑、不失礼 提交于 2019-12-04 15:28:22
This question already has an answer here: When and why will an OS initialise memory to 0xCD, 0xDD, etc. on malloc/free/new/delete? 9 answers ----------------EDIT----------------------- I was grabbing the wrong value for comparison, the cdcdcdcd was coming from somewhere else. I still have my methods throwing exceptions before they are even reached, but my problem lies elsewhere, I wish there was a way to just "unpost" my original question. Thanks for the help. ----------------EDIT----------------------- I have a class (MyClass) I have inherited from some third party class (which in turn

What is a C++11 extension [-Wc++11-extensions]

自闭症网瘾萝莉.ら 提交于 2019-12-03 20:09:42
问题 I need some help understanding where this error is occurring: warning: in-class initialization of non-static data member is a C++11 extension [-Wc++11-extensions] This is the section of the code that it is coming from: typedef struct Hand { bool straight = false; bool flush = false; bool four = false; bool three = false; int pairs = 0; } Hand; 回答1: That's not an error, it's a warning. It tells you that you're only allowed to initialize non-static members of a struct / class starting with the

Why can't I make in-class initialized `const const std::string` a static member

天涯浪子 提交于 2019-12-01 16:22:06
I have the following working code: #include <string> #include <iostream> class A { public: const std::string test = "42"; //static const std::string test = "42"; // fails }; int main(void){ A a; std::cout << a.test << '\n'; } 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

Unique pointer in-class initialization

徘徊边缘 提交于 2019-12-01 05:34:40
Suppose I have a unique_ptr member object that I want to initialize in-class, see the code below. Why do I have to use uniform initialization (curly braces)? The second declaration spits an error, something like so.cpp:10:31: error: expected parameter declarator std::unique_ptr<Foo> upf2(new Foo); ^ so.cpp:10:31: error: expected ')' so.cpp:10:30: note: to match this '(' std::unique_ptr<Foo> upf2(new Foo); ^ 2 errors generated. And I don't think is a most vexing parse issue, at least I don't believe so. #include <memory> class Foo { }; class Bar{ std::unique_ptr<Foo> upf1{new Foo}; // works