constructor

How can I initialize C++ object member variables in the constructor?

大憨熊 提交于 2019-12-27 16:49:47
问题 I've got a class that has a couple of objects as member variables. I don't want the constructors for these members to be called when declared, so I'm trying to hang onto a pointer to the object explicitly. I have no idea what I'm doing. o_O On StackOverflow, I seem to be able to find other examples of object member variables, but usually the constructor is called immediately, like this: class MyClass { public: MyClass(int n); private: AnotherClass another(100); // this constructs AnotherClass

Can I construct a JavaScript object without using the new keyword?

江枫思渺然 提交于 2019-12-27 14:14:26
问题 Here is what I'd like to do: function a() { // ... } function b() { // Some magic, return a new object. } var c = b(); c instanceof b // -> true c instanceof a // -> true b instanceof a // -> true Is it possible? I can make b be an instance of a easily by hooking a into its prototype chain but then I have to do new b() , which is what I'm trying to avoid. Is what I want possible? Update: I feel that it might be possible with judicious use of b.__proto__ = a.prototype . I'm going to experiment

Can the template parameters of a constructor be explicitly specified?

余生长醉 提交于 2019-12-27 11:46:22
问题 A constructor of a class can be a template function. At the point where such a constructor is called, the compiler usually looks at the arguments given to the constructor and determines the used template parameters from them. Is there also some syntax to specify the template parameters explicitly? A contrived example: struct A { template<typename T> A() {} }; Is there a way to instantiate this class? What is the syntax to explicitly specify the constructor's template parameters? My use case

Who deletes the memory allocated during a “new” operation which has exception in constructor?

送分小仙女□ 提交于 2019-12-27 11:45:37
问题 I really can't believe I couldn't find a clear answer to this... How do you free the memory allocated after a C++ class constructor throws an exception, in the case where it's initialised using the new operator. E.g.: class Blah { public: Blah() { throw "oops"; } }; void main() { Blah* b = NULL; try { b = new Blah(); } catch (...) { // What now? } } When I tried this out, b is NULL in the catch block (which makes sense). When debugging, I noticed that the conrol enters the memory allocation

Create a base fluent ordered constructor

白昼怎懂夜的黑 提交于 2019-12-27 03:33:08
问题 EDIT: Ok, seem example is unuseful... I have an ordered fluent constructor that is common to many object (all have tha same properties). Is there a way to put all the code in the same base factory class and have only the final costructor in the derived factory? I use constructor like this .InitCreation() .WithID() .WithPoperty1() .Create() where only the Create() make the new object and ID is a mandatory field. Generics seem me to need rewriting all methods in all factory, instead i want to

using classes with constructor objects

安稳与你 提交于 2019-12-26 05:41:06
问题 I am trying use a class with a constructor object, in another class. But how do I call that class correctly? eg: How do i use Class 1, in Class 2? The example below is creating an object, from a response from an axios call. The _object should be whatever I get from getData() . Im not sure if this is the right approach. But how do i then call this class with the constructor, in another class? Ideally I want to be able to query the properties of the object, and use them in the other class, but

Can I define `default constructor` in Java?

自古美人都是妖i 提交于 2019-12-25 19:42:09
问题 My question below is rather theoretical then practical. From many Java resources available online I found out that a default constructor for a class has below specification: takes no arguments has no throws clauses has an empty body Java language specification does not provide definition for default constructor , it only states that If a class (definition) contains no constructor declarations, then a default constructor is implicitly declared (by a compiler). Please notice that wording

Can I define `default constructor` in Java?

佐手、 提交于 2019-12-25 19:41:23
问题 My question below is rather theoretical then practical. From many Java resources available online I found out that a default constructor for a class has below specification: takes no arguments has no throws clauses has an empty body Java language specification does not provide definition for default constructor , it only states that If a class (definition) contains no constructor declarations, then a default constructor is implicitly declared (by a compiler). Please notice that wording

C++: friend as main in class

こ雲淡風輕ζ 提交于 2019-12-25 19:36:25
问题 Can main function become friend function in C++ ? #include "stdafx.h" #include <iostream> using namespace std; class A { public: A():i(10){} private: int i; friend int main(); }; int main() { A obj; cout<<obj.i; return 0; } 回答1: Can main function become friend function in C++ ? Yes, it can. The friend declaration in your class A grants function main() the right of accessing the name of its non-public data members (in this case, i ): friend int main(); The object obj is default-constructed,

C++: friend as main in class

杀马特。学长 韩版系。学妹 提交于 2019-12-25 19:35:39
问题 Can main function become friend function in C++ ? #include "stdafx.h" #include <iostream> using namespace std; class A { public: A():i(10){} private: int i; friend int main(); }; int main() { A obj; cout<<obj.i; return 0; } 回答1: Can main function become friend function in C++ ? Yes, it can. The friend declaration in your class A grants function main() the right of accessing the name of its non-public data members (in this case, i ): friend int main(); The object obj is default-constructed,