constructor

Calling function using 'new' is less expensive than without it?

Given this very familiar model of prototypal construction: function Rectangle(w,h) { this.width = w; this.height = h; } Rectangle.prototype.area = function() { return this.width * this.height; }; Can anyone explain why calling new Rectangle(2,3) is consistently 10x FASTER than calling Rectangle(2,3) without the 'new' keyw...

How to handle 'this' pointer in constructor?

I have objects which create other child objects within their constructors, passing 'this' so the child can save a pointer back to its parent. I use boost::shared_ptr extensively in my programming as a safer alternative to std::auto_ptr or raw pointers. So the child would have code such as shared_ptr<Parent>, and boost provides the shar...

Is it a good or bad practice to call instance methods from a java constructor?

There are several different ways I can initialize complex objects (with injected dependencies and required set-up of injected members), are all seem reasonable, but have various advantages and disadvantages. I'll give a concrete example: final class MyClass { private final Dependency dependency; @Inject public MyClass(Dependency de...

How do JVM's implicit memory barriers behave when chaining constructors?

Referring to my earlier question on incompletely constructed objects, I have a second question. As Jon Skeet pointed out, there's an implicit memory barrier in the end of a constructor that makes sure that final fields are visible to all threads. But what if a constructor calls another constructor; is there such a memory barrier in the ...

C++ using this pointer in constructors

In c++, during a class constructor, I started a new thread with 'this' pointer as a parameter which will be used in the thread extensively (say, calling member functions). Is that a bad thing to do? Why and what are the consequences? Edit: my thread start process is at the end of the constructor. Thanks, Gil. ...

C++: Construction and initialization order guarantees

Hi, I have some doubts about construction and initialization order guarantees in C++. For instance, the following code has four classes X, Y, Z and W. The main function instantiates an object of class X, which contains an object of class Y, and derives from class Z, so both constructors will be called. Additionally, the const char* para...

RhinoMocks Stub return real instance

I'm trying to use RhinoMocks to stub out a third party component. The third party component looks like the following. public class connection { public connection(string host,int port) {} public void Submit(message msg) {} } public class message { public message(string recipient) {} { When I attempt to use a s...

Calling a constructor to reinitialize variables doesn't seem to work?

I wanted to run 1,000 iterations of a program, so set a counter for 1000 in main. I needed to reinitialize various variables after each iteration, and since the class constructor had all the initializations already written out - I decided to call that after each iteration, with the result of each iteration being stored in a variable in ...

MVVM load data during or after ViewModel construction?

My generic question is as the title states, is it best to load data during ViewModel construction or afterward through some Loaded event handling? I'm guessing the answer is after construction via some Loaded event handling, but I'm wondering how that is most cleanly coordinated between ViewModel and View? Here's more details about my ...

Constructor with fewer arguments from a constructor

I have Constructor Tree(int a, int b, int c) and second Constructor Tree(int a, int b, int c, String s). How to load second constructor from first just to save writing all the logics? I thought about something like this but it gives me 'null' object. public Tree(int a, int b, int c){ Tree t1 = new Tree(a, b, c, "randomString"); } ...

C#: Struct Constructor: "fields must be fully assigned before control is returned to the caller."

Here is a struct I am trying to write: public struct AttackTraits { public AttackTraits(double probability, int damage, float distance) { Probability = probability; Distance = distance; Damage = damage; } private double probability...

What is the meaning of ": base" in the constructor definition ?

What is the meaning of ": base" in the costructor of following class(MyClass) ? Please explain the concept behind constructor definition given below for class MyClass. public class MyClass: WorkerThread { public MyClass(object data): base(data) { // some code } } public abstract class Worker...

Specifying properties when initialising

void Foo() { string ID = "test"; var testctrl = new Control() {ID = (ID**!=null?ID**:ID)}; } Is it possible to get the value of ID** in the code above? The problem is they both have the same property names. Please ignore the fact the specific ID assignment is pointless, im just using it as an example. ...

In a WPF Application, What happens after my code in the main window constructor is executed?

I'm wondering what happens after the constructor is done executing my code, because the constructor is taking like 10 seconds to run on a cold start up, but according to the profiler, my code is done executing in like 2 seconds. Also stepping through the code in the debugger, after the last line of my constructor, I sit there and wait f...

Calling MethodBase's Invoke on a constructor (reflection)

Hi everyone. First of all, sorry if this has been asked before. I've done a pretty comprehensive search and found nothing quite like it, but I may have missed something. And now to the question: I'm trying to invoke a constructor through reflection, with no luck. Basically, I have an object that I want to clone, so I look up the copy co...

Is it true that the assigned final object field may still be null inside a constructor?

Is it true that the assigned final object field may still be null inside a constructor? class MyClass { private final Object obj = new Object(); public MyClass() { System.out.println(obj); // may print null? } } if yes, isn't this a bug? ...

When should I use a factory to wrap a constructor in Perl?

Why to use factory to wrap a constructor in Perl? An example would help. ...

How to inherit constructors with arguments in .NET?

I have a "MustInherit" .NET class which declares a constructor with an integer parameter. However, Visual Studio gives me an error when I create any derived class stating that there is no constructor that can be called without any arguments. Is it possible to inherit the constructor with arguments? Right now, I have to use Public Sub N...

How to avoid code repetition initializing final properties?

public class Code{ //many properties //... final String NEWLINE;// ohh a final property! void creation() //this method is for avoid repetition of code { //final initialization can't be put here =( Source= new StringBuffer(); //many other commons new's .. //... } Code() { NEWLINE = System.getProperty("line...

Why is the base() constructor not necessary?

Hello, I have a class structure like abstract class Animal { public Animal(){ //init stuff.. } } class Cat : Animal { public Cat(bool is_keyboard) : base() //NOTE here { //other init stuff } } Now then, look at the noted line. If you remove : base() then it will compile without an error. Why is this? Is there a wa...