constructor

“Leaking this” from a Design Standpoint

一世执手 提交于 2019-12-13 15:47:16
问题 Warning: Leaking "this" in constructor I keep running into this, and I have a nagging feeling that it's because my design is wrong or not optimal. I understand that this warning is bringing to my attention the fact that I am allowing access to an object that is potentially not fully initialized. Let's say that I need a Frame that HAS and requires a List (Frame(List list)). In List, I might want to do something such as add(). In order to make sure Frame knows as little about List as possible

Does `const_iterator` really need to be a different class than `iterator`?

爷,独闯天下 提交于 2019-12-13 15:33:03
问题 Let say I define some kind of container A : struct A { iterator begin(){ return iterator(this,0); } const iterator cbegin() const { return iterator(this, last());} //... }; Suppose now I want to declare the iterator (part of A): struct A::iterator { iterator ( A* ptr, size_t idx){}; //... }; Which I would use like: const A a; A::iterator it = a.cbegin(); That does not work because the pointer passed to the constructor of iterator is non-const. The ideal solution would be something like a

Stanford-parser in Ruby does not create Preprocesser

天涯浪子 提交于 2019-12-13 15:22:42
问题 I am trying to use Stanford-parser for Ruby and get a RuntimeError: Constructor not found I had to install 'rbj' and 'treebank' gems to get it running. Now I can require 'stanfordparser' but can't get to preproc = StanfordParser::DocumentPreprocessor.new The funciton that returns the error is here (ruby-1.9.3-p0/gems/stanfordparser-2.2.0/lib/java_object.rb:40:in `new'): def initialize(obj, *args) @java_object = obj.class == String ? Rjb::import(obj).send(:new, *args) : obj end I saw a couple

Are there problems with replacing a Javascript constructor's .prototype rather than adding to it?

雨燕双飞 提交于 2019-12-13 14:27:42
问题 I've come across another developer's code which does something like this to define a Javascript type: function Ninja() {} Ninja.prototype = { swingSword: function(){ return true; } } when the more conventional manner is something like: function Ninja() {} Ninja.prototype.swingSword = function() { return true; } The first form mostly works, in that you can create a new instance and call the swingSword method, and instanceof is accurate: var ninja = new Ninja(); ninja.swingSword(); // returns

Static Class Data Members and Constructors

眉间皱痕 提交于 2019-12-13 14:14:35
问题 How do I access a static member in a class with all static methods? I want to have a group of related functions but also have some important data members initialized before any of these functions are called. I thought a class with only static members would be the way to go. Compiler in VS2008 doesn't like me trying to access "a". Surely I'm missing something small but still very confused. :P (Even without the invalid access of "a" the constructor isn't called when calling testMethod() from

What conditions cause object instantiation to return null? [duplicate]

回眸只為那壹抹淺笑 提交于 2019-12-13 13:48:42
问题 This question already has answers here : Can constructor return a null object? (9 answers) Closed 3 years ago . Is it possible for the following line to return null? MyClass obj = new MyClass(); If so, what conditions would cause a return value of null? 回答1: It's impossible for new to return null, assuming the VM is functioning correctly. From section 15.9.4 of the Java Language Specification: The value of a class instance creation expression is a reference to the newly created object of the

Using 'this' as a parameter to a method call in a constructor

感情迁移 提交于 2019-12-13 13:04:08
问题 I have a constructor like as follows: public Agent(){ this.name = "John"; this.id = 9; this.setTopWorldAgent(this, "Top_World_Agent", true); } I'm getting a null pointer exception here in the method call. It appears to be because I'm using 'this' as an argument in the setTopWorldAgent method. By removing this method call everything appears fine. Why does this happen? Has anyone else experienced this? 回答1: You can pass this to methods, but setTopWorldAgent() cannot be abstract. You can't make

Using constructor where function expected

╄→尐↘猪︶ㄣ 提交于 2019-12-13 12:59:16
问题 Having two simple classes taking Int as an argument: case class Foo(i: Int) class Bar(j: Int) I can say: List(1,2,3) map Foo Which works fine and is equivalent to a bit more verbose: List(1,2,3) map {Foo(_)} However Bar (because it is not a case class?) cannot be used in the same construct: List(1,2,3) map Bar error: not found: value Bar List(1,2,3) map Bar ^ Is there some special syntax to reference any constructor and take advantage of eta expansion? List(1,2,3) map {new Bar(_)} seems a bit

Constructor requiring more than one for subclass super

核能气质少年 提交于 2019-12-13 12:52:23
问题 Please help me find errors from this code. I'm still new and I don't know if this is correct or not. I do have one error. This is the error: constructor Person in class Person cannot be applied to given types; super(); ^ required: String,String,String found: no arguments reason: actual and formal argument lists differ in length This is my code: import java.util.*; public class Person { //Data fields private String lastName; private String middleInitial; private String firstName; /

User defined constructor for Fortran derived type instance

独自空忆成欢 提交于 2019-12-13 12:29:46
问题 This is my second question related to Fortran (I use C++, so forgive me my way of thinking). I want to use OOP, to say, derived type in Fortran whenever appropriate. In C++, you can use user defined constructor such as https://msdn.microsoft.com/en-us/library/s16xw1a8.aspx Here in Fortran, things are different. The first thing I tried is from here: https://www.ibm.com/developerworks/community/blogs/b10932b4-0edd-4e61-89f2-6e478ccba9aa/entry/object_oriented_fortran_user_defined_constructors2