constructor

NullReferenceException when accessing a property outside of the constructor

In this class, I am setting elp to ElType in the constructor. I can access properties of elp fine when in the constructor (the // ... bit is where I'm accessing elp's properties), but when I try to access elp in another method - ucp() - my program crashes with NullReferenceException. I can't figure out what I'm doing wrong here, althou...

Copy constructor in python

Is there a copy constructor in python ? If not what would I do to achieve something similar ? The situation is that I am using a library and I have extended one of the classes there with extra functionality and I want to be able to convert the objects I get from the library to instances of my own class. ...

Templated copy-constructor fails with specific templated type

As some of my code required implicit conversion between matrices of different types (e.g. Matrix<int> to Matrix<double>), I defined a templated copy constructor Matrix<T>::Matrix(Matrix<U> const&) instead of the standard Matrix<T>::Matrix(Matrix<T> const&): template <typename T> class Matrix { public: // ... template <typename U...

How can I intercept this constructor call in Groovy?

In a script a method receives a parameter of type File, and sends it to the constructor of File. This blows up because File doesn't have a constructor that takes another file as a parameter. How can I intercept this call, and modify the parameter to parameter.absolutePath ? For example : def x = new File("some_file") ... def meth(de...

Order of constructors for a C# class: parameterized, default, and static?

Suppose I have a class with 3 constructors, a default (no argument) constructor, a parameterized constructor, and a static constructor. like this: public MyClass() { ... } public MyClass(string arg) : this() { ... } static MyClass() { ... } Supposing I invoke the parameterized constructor, in what order do these constructors ex...

Static initialization guarantees singleton thread safety? (C#)

Hello, Jon Skeet's excellent article at http://www.yoda.arachsys.com/csharp/singleton.html and other articles I've read make it clear that that double-check locking doesn't work in both C# and Java unless one explicitly marks the instance as "volatile." If you don't, the check of comparing it to null could possibly return false even tho...

C++: When (and how) are C++ Global Static Constructors Called?

Hi, I'm working on some C++ code and I've run into a question which has been nagging me for a while... Assuming I'm compiling with GCC on a Linux host for an ELF target, where are global static constructors and destructors called? I've heard there's a function _init in crtbegin.o, and a function _fini in crtend.o. Are these called by c...

What does a colon following a C++ constructor name do?

What does the colon operator (":") do in this constructor? Is it equivalent to MyClass(m_classID = -1, m_userdata = 0);? class MyClass { public: MyClass() : m_classID(-1), m_userdata(0) { } int m_classID; void *m_userdata; }; ...

Wrapping exceptions thrown in super/this constructor calls into other exceptions

I want the api of my module to only throw MyPackageSpecificException whenever anything goes wrong and the module unable to perform its task. (The original exception will be given as the cause of the MyPackageSpecificException). Now, for one constructor I needed an URL as a parameter to locate a resource. I would also like to make an alt...

Using RhinoMocks, how do you mock or stub a concrete class without an empty constructor?

Mocking a concrete class with Rhino Mocks seems to work pretty easy when you have an empty constructor on a class: public class MyClass{ public MyClass() {} } But if I add a constructor that takes parameters and remove the one that doesn't take parameters: public class MyClass{ public MyClass(MyOtherClass instance) {} } I...

C# - why is it not possible to access 'this' in a field initializer?

Why does this produce a compiler error: class Foo { public Bar Baz = new Bar(this); } But this does not: class Foo { public Bar Baz; public Foo() { this.Baz = new Bar(this); } } Conceptually the two are equivalent, are they not? ...

How do you fight growing parameter list in class hierarchy?

I have a strong feeling that I do not know what pattern or particular language technique use in this situation. So, the question itself is how to manage the growing parameter list in class hierarchy in language that has OOP support? I mean if for root class in the hierarchy you have, let's say 3 or 4 parameters, then in it's derived clas...

Calling constructor overload when both overload have same signature

Consider the following class, class Foo { public Foo(int count) { /* .. */ } public Foo(int count) { /* .. */ } } Above code is invalid and won't compile. Now consider the following code, class Foo<T> { public Foo(int count) { /* .. */ } public Foo(T t) { /...

How to implement a class constructor accepting an array of different types as an argument in Java

I have the following class: private class Info{ public String A; public int B; Info(){}; public OtherMethod(){}; private PrivMethod(){}; } And I want to create an array of this class, but I want to provide a two dimensional array as an argument to the constructor, ie: Info[] I = new Info({{"StringA", 1}, {"Strin...

JavaScript: Can a constructor function create a documentElement object?

I want to make a constructor function that creates a documentElement object. As an example, consider the new Audio() constructor - it creates a documentElement object, and if you pass it some variables it populates the new documentElement with attributes. It doesn't insert it into the DOM, it simply creates the object. So, the questio...

Does the memory get released when I throw an exception?

Hi, I was debating with some colleges about what happens when you throw an exception in a dynamically allocated class. I know that malloc gets called, and then the constructor of the class. The constructor never returns, so what happens to the malloc? Consider the following class B { public: B() { cout << "B::B()" ...

Is this possible to invoke a constructor dynamically using relfection if there is no empty default constructor?

I'm using GetParameter to determine what parameters the constructor needs. I can get a list of them. Now I want to invoke the ctor. Is this possible if there is no empty one? ...

How to find the function name of namespaced constructors in javascript?

For example, compare these two: function Person(name) { this.name = name; } var john = new Person('John'); console.log(john.constructor); // outputs: Person(name) var MyJSLib = { Person : function (name) { this.name = name; } } var john2 = new MyJSLib.Person('John'); console.log(john2.constructor); // outputs: function() The fi...

sizeof() And Template Argument In ctor / Non-ctor Function

I hit a snag today... I wanted to define a small templated helper class: template<class T> CMyClass { public : CMyClass() { size_t iSize = sizeof(T); } // Allowed. size_t GetElementSize() const { return sizeof(T); } // C2027. }; and of course, it wouldn't compile (C2027). My question was, is it possible to get the size of t...

How to store a factory used in a derived class to initialize its base class?

Assuming you had some kind of factory-created resource that would still belong to the factory after construction like this: public class Resource : IDisposable { public void Dispose() { /* ... */ } } public class ResourceManager : IDisposable { public Resource Load() { /* create resource and add to list */ } public void Dispose()...