constructor

How constructors can initialize final fields with complex algorithm?

倖福魔咒の 提交于 2020-01-02 20:08:27
问题 I am writing an immutable class Vector3. class Vector3 { final num x, y, z; Vector3(this.x, this.y, this.z); num get length => ...some heavy code } Now, I want to write a constructor that computes the unit vector from another vector. I want to do that according to the Dart's recommendations (avoid writing a static method when we create a new object). My problem is that I can't write this constructor because the final fields must be initialized before the constructor's body and cannot be

Constructor being called with less arguments in C++

半城伤御伤魂 提交于 2020-01-02 19:52:49
问题 I have class Foo with a constructor as given: class Foo { public: Foo(int w, char x, int y, int z); ... }; int main() { Foo abc (10, 'a'); } Can I use that constructor like this? When constructor signature do not match? So How do I give default value? 回答1: Not unless the parameters at the tail of the signature have defaults, for example: class Foo { public: Foo(int w, char x, int y=5, int z=0); ... }; If there are defaults, then you can supply only the non-defaulted parameters, and optionally

Passing mysqli to class for function use

南楼画角 提交于 2020-01-02 19:46:55
问题 Probably asked many times but I am hard-headed. I have the following class to manage a MySQL db. class blog { function show ($mysqli) { // Code working on $mysqli here } } Since I will be using $mysqli in many functions inside of this class I read that I can create constructors in order to pass the $mysqli variable to the class and use it inside of each function so I can do something like: $blog = new blog($mysqli); $blog -> show(); Is this possible? 回答1: This is called Dependency injection.

Have fixed parameters in Java constructor - possible?

て烟熏妆下的殇ゞ 提交于 2020-01-02 10:22:31
问题 I've defined an object in Java - as far as the Java is concerned, they're the same thing, but as far as the data which populates them is concerned, they can be one of three types (wildly named 1,2,3 with 0 for the "root"). What I'd really like to be able to do is have four constructors defined, as they need slightly different params for each type. I could do it with strategic nulls, but it seems like the wrong thing to do. What I'd love to have is something like: public MenuNode(int type = 1,

constructor calling order with composition

巧了我就是萌 提交于 2020-01-02 08:54:49
问题 I have a class A and Class B . Class C derives from Class B and has Class A object as composition . http://ideone.com/JGT48M #include "iostream" using namespace std; class A { int i; public: A(int ii) : i(ii) { cout << "\n Constructor of A is called \n"; } ~A() { cout << "\n destructor of A is called \n"; } void f() const {} }; class B { int i; public: B(int ii) : i(ii) { cout << "\n Constructor of B is called \n"; } ~B() { cout << "\n destructor of B is called \n"; } void f() const {} };

Need for try catch within a constructor

淺唱寂寞╮ 提交于 2020-01-02 07:51:15
问题 The link http://gotw.ca/gotw/066.htm states that Moral #1: Constructor function-try-block handlers have only one purpose -- to translate an exception. (And maybe to do logging or some other side effects.) They are not useful for any other purpose. While http://www.parashift.com/c++-faq-lite/exceptions.html#faq-17.8 If a constructor throws an exception, the object's destructor is not run. If your object has already done something that needs to be undone (such as allocating some memory, opening

Scala overloaded constructors and super

爷,独闯天下 提交于 2020-01-02 05:43:09
问题 I can't understand how to develop Scala code similar to the following on Java: public abstract class A { protected A() { ... } protected A(int a) { ... } } public abstract class B { protected B() { super(); } protected B(int a) { super(a); } } public class C extends B { public C() { super(3); } } while it's clear how to develop C class, I can't get how to receive B. Help, please. P.S. I'm trying to create my own BaseWebPage derived from wicket WebPage which is a common practice for Java 回答1:

spring-security writing a custom PermissionEvaluator - how to inject a DAO service?

*爱你&永不变心* 提交于 2020-01-02 05:31:11
问题 I'm working with Spring-Security and I need to implement my own PermissionEvaluator (following the answer to my other question. However looking at the standard implementation AclPermissionEvaluator here I notice, that the DAO is set via the constructor. If I declare my custom PermissionEvaluator like this: <global-method-security secured-annotations="enabled" pre-post-annotations="enabled"> <expression-handler ref="expressionHandler"/> </global-method-security> <beans:bean id=

Delaying trait initialization

帅比萌擦擦* 提交于 2020-01-02 04:49:26
问题 I need a smart mechanism for component composition which allows mixed in traits to initialize after the composed component. The following throws a NullPointerException : class Component { def addListener(pf: PartialFunction[Any, Unit]) {} } trait DynamicComponent { protected def component: Component component.addListener { case x => } } class Foo extends DynamicComponent { protected val component = new Component } new Foo // -> NullPointerException The following things are not options for me:

Is it acceptable to return null from a factory constructor in Dart?

安稳与你 提交于 2020-01-02 04:36:06
问题 I've been writing some code in Dart. I really love the factory constructor, but I'm afraid that I'm abusing it's usefulness. In particular, when I write a value object class, I sometimes return null if the validation fails. class EmailAddress { static final RegExp _regex = new RegExp(...); final String _value; factory EmailAddress(String input) { return _regex.hasMatch(input) ? new EmailAddress._internal(input) : null; } const EmailAddress._internal(this._value); toString() => _value; } At