constructor

Objective C two-phase construction of objects

I've been reading up on RAII and single vs. two-phase construction/initialization. For whatever reason, I was in the two-phase camp up until recently, because at some point I must have heard that it's bad to do error-prone operations in your constructor. However, I think I'm now convinced that single-phase is preferable, based on quest...

How to pass a function pointer that points to constructor?

I'm working on implementing a reflection mechanism in C++. All objects within my code are a subclass of Object(my own generic type) that contain a static member datum of type Class. class Class{ public: Class(const std::string &n, Object *(*c)()); protected: std::string name; // Name for subclass Object *(*create)(); // Po...

easiest way to make a class inherit constructors (C#)

Yeah, sorry about asking a stupid n00b question. So I have a C# program. I have a class class foo { public int bar, wee, someotherint, someyetanotherint; public foo() { this.bar = 1: this.wee = 12; this.someotherint = 1231; this.someyetanotherint = 443; } } And I want to make a cla...

Calling virtual functions inside constructors

Suppose I have two C++ classes: class A { public: A() { fn(); } virtual void fn() { _n = 1; } int getn() { return _n; } protected: int _n; }; class B : public A { public: B() : A() {} virtual void fn() { _n = 2; } }; If I write the following code: main() { B b; int n = b.getn(); } One might expect that n is set ...

C# Constructor Design

I have a class which you pass in a folder and then it goes off and processes a lot of data within the specified folder. For instance: MyClass myClass = new MyClass(@"C:\temp"); Now what it does it goes off and reads say a couple thousand files and populates the class with data. Should I move this data out from the constructor and ha...

How can I force a C# constructor to be a factory?

The code below consists of two classes: SmartForm (simple model class) SmartForms (plural class that contains a collection of SmartForm objects) I want to be able to instantiate both singular and plural classes like this (i.e. I don't want a factory method GetSmartForm()): SmartForms smartForms = new SmartForms("all"); SmartForm sm...

ASP.Net MissingMethodException - "ctor" method not found

We are getting intermittent problems on a production server that we cannot recreate. There are two very strange things about the issue. Firstly it's a method not found error on the constructor (ctor) for an exception handling helper class and secondly we have custom errors switched on for remote users and this property is being ignored....

How to force implmentation of multiple constructors

I'm creating a base class Node that essentially wraps instances of another class from a program I am writing a plugin for, BaseAppObject. Properties of Node and any derivation of Node store their properties in the BaseAppObject through two methods BaseAppObject.SetUserString(key,value) and BaseAppObject.GetUserString(key,value). There a...

Variable initialising and constructors

I am currently taking a c++ course and trying to get a deep understanding of the whole thing. I came up with some theories, it would be great if somebody could confirm them: Every variable (local,global,staic,member and non-member) is guaranteed to have its ctor called before first use The ctors of primitives like int are essentially n...

Can I call a overloaded constructor from another constructor of the same class in C#?

Can I call a overloaded constructor from another constructor of the same class in C#? ...

Unit testing for exceptions in Python constructor

Hello, I'm just a beginner in Python and programming in general, and have some questions about the unittest module. I have a class, and in the __init__ method I am doing some assertions to check for bad arguments. I would like to create a unittest which checks for such AssertionError when creating new instances. In the unittest modul...

How can I create an object whose derived class is specified implicitly by the creation properties?

I'm looking for a pattern for the following. (I'm working in Perl, but I don't think the language matters particularly). With a parent class Foo, and children Bar, Baz, Bazza. One of the methods for constructing a Foo is by parsing a string, and part of that string will implicitly specify which class is to be created. So for example i...

Using this() in C# Constructors

Hello, I have been trying to figure out if there are any differences between these constructors. Assuming there is a Foo() constructor that takes no arguments, are all these constructors going to have the same result? Example 1 public Foo() : this() { blah; blah; blah; } Example 2 public Foo() { this(); ...

Changing a constructor param type breaks class in another jar

I have the following class in a common jar: public class Common { public Common(List list) { ... } } I then change the constructor parameter from a List to a Collection as follows: public class Common { public Common(Collection collection) { ... } } Rebuilding the common jar and running the system causes a...

Is it possible to extend a class with only private constructors in Java?

For unit testing purposes I'm trying to write a mock object of a class with no constructors. Is this even possible in Java, of is the class simply not extensible? ...

Is there a way to derive from a class with an internal constructor?

I'm working with a 3rd party c# class that has lots of great methods and properties - but as time has gone by I need to extend that class with methods and properties of my own. If it was my code I would just use that class as my base class and add my own properties and method on top - but this class has an internal constructor. (In my ...

Is there any justifiable reason to use new and a constructor on a number class in Java?

Is there any justifiable reason to in Java something like Long l = new Long(SOME_CONSTANT) This creates an extra object and is tagged by FindBugs, and is obviously a bad practice. My question is whether there is ever a good reason to do so? I previously asked this about String constructors and got a good answer, but that answer does...

Can assignment be done before constructor is called?

A comment to http://stackoverflow.com/questions/945232/whats-wrong-with-this-fix-for-double-checked-locking says: The issue is that the variable may be assigned before the constructor is run (or completes), not before the object is allocated. Let us consider code: A *a; void Test() { a = new A; } To allow for more for...

How can I tell if an object is statically or dynamically allocated on the constructor?

I have an object that requires a slightly different construction wether it's instance is staticly or dynamically allocated. The object should only have a single default constructor. So having two constructors, one for each case, and having the user explicitly select the proper constructor is out of the question. Is there any proper way ...

Whats the advantage of using a constructor function in a JavaScript object?

What's the advantage of using a constructor function like so: var MyLibrary = new function(){ var self = this; self.MyLibrary = function(){ // init code } } Instead of simply writing code inside the object? var MyLibrary = new function(){ // init code } ...