constructor

zeroing derived struct using memset

倾然丶 夕夏残阳落幕 提交于 2019-12-19 11:26:51
问题 I want to zero out all members of a derived structure. There are hundreds of members and more are added every once in a while so I feel that initializing them explicitly is error-prone. The structures have no virtual functions and all the member fields are built-in. However, they are not POD by virtue of having non-trivial constructors. Apart from the standard frowning on the practice, do you see any issues with the following? struct Base { // Stuff }; struct Derived : public Base { //

zeroing derived struct using memset

强颜欢笑 提交于 2019-12-19 11:26:37
问题 I want to zero out all members of a derived structure. There are hundreds of members and more are added every once in a while so I feel that initializing them explicitly is error-prone. The structures have no virtual functions and all the member fields are built-in. However, they are not POD by virtue of having non-trivial constructors. Apart from the standard frowning on the practice, do you see any issues with the following? struct Base { // Stuff }; struct Derived : public Base { //

Template constructor weirdness [duplicate]

半城伤御伤魂 提交于 2019-12-19 10:48:12
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Can the template parameters of a constructor be explicitly specified? following up on my previous question, (I found this situation in edit 2) Laid out simple in code: #include <iostream> struct Printer { Printer() { std::cout << "secret code" << std::endl; } }; template <class A> struct Class { template <class B, class C> Class(B arg) { C c; /* the 'secret code' should come from here */ std::cout << arg << std:

default arguments in constructor

浪子不回头ぞ 提交于 2019-12-19 10:42:42
问题 Can I use default arguments in a constructor like this maybe Soldier(int entyID, int hlth = 100, int exp = 10, string nme) : entityID(entyID = globalID++), health(hlth), experience(exp), name(nme = SelectRandomName(exp)) { } I want for example exp = 10 by default but be able to override this value if I supply it in the constructor otherwise it should use the default. How can I do this, I know my approach does not work.... If I supply any value in the initialization list no matter whatever I

Java: Difference between initializing by constructor and by static method?

非 Y 不嫁゛ 提交于 2019-12-19 08:02:14
问题 This might just be a question of personal taste and workflow, but in case it's more than that, I feel I should ask anyway. In Java, what differences are there between creating an instance via constructor and via a static method (which returns the instance)? For example, take this bit of code from a project I'm working on (written up by hand at time of posting, so some shortcuts and liberties are taken): Plugin main; Map<int, int> map; public Handler(Plugin main) { this.main = main; } public

Java: Difference between initializing by constructor and by static method?

丶灬走出姿态 提交于 2019-12-19 08:02:09
问题 This might just be a question of personal taste and workflow, but in case it's more than that, I feel I should ask anyway. In Java, what differences are there between creating an instance via constructor and via a static method (which returns the instance)? For example, take this bit of code from a project I'm working on (written up by hand at time of posting, so some shortcuts and liberties are taken): Plugin main; Map<int, int> map; public Handler(Plugin main) { this.main = main; } public

Active member of an union, uniform initialization and constructors

醉酒当歌 提交于 2019-12-19 07:23:27
问题 As the (Working Draft of) C++ Standard says: 9.5.1 [class.union] In a union, at most one of the non-static data members can be active at any time , that is, the value of at most one of the non-static data members can be stored in a union at any time. [...] The size of a union is sufficient to contain the largest of its non-static data members. Each non-static data member is allocated as if it were the sole member of a struct. All non-static data members of a union object have the same address

Exceptions in constructors

微笑、不失礼 提交于 2019-12-19 06:42:51
问题 In C++, the lifetime of an object begins when the constructor finishes successfully. Inside the constructor, the object does not exist yet. Q: What does emitting an exception from a constructor mean? A: It means that construction has failed, the object never existed, its lifetime never began. [source] My question is: Does the same hold true for Java? What happens, for example, if I hand this to another object, and then my constructor fails? Foo() { Bar.remember(this); throw new

C# default value in constructor same as two constructors for serialization

点点圈 提交于 2019-12-19 05:57:29
问题 When I provide a constructor with a default value public MyClass(string description = null) { .... } is this equivalent to public MyClass() { .... } public MyClass(string description) { .... } in terms of Serialization . In other words, is a default constructor available ? Practically it is, but will I face some issues when I use serialization? 回答1: No. It unfortunately is not a default constructor. When you write: public MyClass(string description = null) { .... } You're actually making a

Calling a virtual base class's overloaded constructor

随声附和 提交于 2019-12-19 05:44:31
问题 Is there a (practical) way to by-pass the normal (virtual) constructor calling order? Example: class A { const int i; public: A() : i(0) { cout << "calling A()" << endl; } A(int p) : i(p) { cout << "calling A(int)" << endl; } }; class B : public virtual A { public: B(int i) : A(i) { cout << "calling B(int)" << endl; } }; class C : public B { public: C(int i) : A(i), B(i) { cout << "calling C(int)" << endl; } }; class D : public C { public: D(int i) : /*A(i), */ C(i) { cout << "calling D(int)"