constructor

How do you call a constructor for global objects, for arrays of objects, and for objects inside classes/structs?

How would you call the constructor of the following class in these three situations: Global objects, arrays of objects, and objects contained in another class/struct? The class with the constructor (used in all three examples): class Foo { public: Foo(int a) { b = a; } private: int b; }; And here are my attem...

C# member variable initialization; best practice?

Is it better to initialize class member variables on declaration private List<Thing> _things = new List<Thing>(); private int _arb = 99; or in the default constructor? private List<Thing> _things; private int _arb; public TheClass() { _things = new List<Thing>(); _arb = 99; } Is it simply a matter of style or are there perform...

c++ call constructor from constructor

Hi *, As an c# developer I'm used to run through constructors: class Test { public Test() { DoSomething(); } public Test(int count) : this() { DoSomethingWithCount(count); } public Test(int count, string name) : this(count) { DoSomethingWithName(name); } } Is there a way to do this in...

DDD + Public Parameterless Constructors - Should They Exist?

One of the tenants of DDD is to not allow your objects to enter an invalid state. To me this means there shouldn't be a public parameterless constructor because that's going to be an object in an invalid state 99% of the time. Is this a good way to move forward? It becomes a huge PITA when you just want to new-up a class real quick. ...

Can you invoke an instantiated object's class constructor explicity in C++?

After creating a instance of a class, can we invoke the constructor explicitly? For example class A{ A(int a) { } } A instance; instance.A(2); Can we do this? ...

How can I pass a class name as an argument to an object factory in cocoa?

I am working on an object factory to keep track of a small collection of objects. The objects can be of different types, but they will all respond to createInstance and reset. The objects can not be derived from a common base class because some of them will have to derive from built-in cocoa classes like NSView and NSWindowController. I...

Initializing a union with a non-trivial constructor

I have a structure which I create a custom constructor to initialize the members to 0's. I've seen in older compilers that when in release mode, without doing a memset to 0, the values are not initialized. I now want to use this structure in a union, but get errors because it has a non-trivial constructor. So, question 1. Does the de...

How can I force the base constructor to be called in C#?

I have a BasePage class which all other pages derive from: public class BasePage This BasePage has a constructor which contains code which must always run: public BasePage() { // Important code here } I want to force derived classes to call the base constructor, like so: public MyPage : base() { // Page specific code h...

Call a factory from Constructor to get a new version of "this"

I may be going about this backwards... I have a class which is like a document and another class which is like a template. They both inherit from the same base class and I have a method to create a new document from a template (or from another document, the method it is in the base class). So, if I want to create a new document from a t...

C++ Constructor coding errors

I just stumbled across this bug in some legacy code: class MyAPIHandler { private: int handle; public: void MyApiHandler() // default constructor { handle = 42; }; }; It compiles fine, with no warnings - but the behaviour wasn't what I intended, because the constructor name is misspelt. This by itself wo...

Do you use TestInitialize or the test class constructor to prepare each test? and why?

This question regards unit testing in Visual Studio using MSTest (this is important, because of MSTest's execution order). Both the method marked [TestInitialize] and the test class constructor will run before each test method. So, the question is, what do you tend to do in each of these areas? Do you avoid performing certain activities...

Calling Overriden Constructor and Base Constructor in C#

I have two classes, Foo and Bar, that have constructors like this: class Foo { Foo() { // do some stuff } Foo(int arg) { // do some other stuff } } class Bar : Foo { Bar() : base() { // some third thing } } Now I want to introduce a constructor for Bar that takes an int, but I wa...

How can a member know in what class instance it is constructed?

class C { public T x; }; Is there an elegant way for the constructor of x to know implicitly in what instance of C it is constructing? I've implemented such behavior with some dirty inelegant machinery. I need this for my sqlite3 wrapper. I don't like all wrappers I've seen, their API IMO ugly and inconvenient. I wa...

: this(foo) syntax in C# constructors?

Every now and then, I bump into syntax that I've seen before, but never used. This is one of those times. Can someone explain the purpose of ":this" or ":base" following a C# constructor method? For example: public MyClass(SomeArg arg) : this(new SomethingElse(), arg) { } My gut feeling is that it is used to map a default argument o...

How can I have over loaded constructor in Php5?

I have two constructor : function clsUsagerEmailUserName($nickName, $email) { $this->nickName = $nickName; $this->email = $email; } function clsUsagerEmailUserName($email) { $this->email = $email; } But this is not working? What's wrong, isn't supposed to be OO at this version of ...

Inheriting constructors

...

Prevent creation of class whose member functions are all static

All the member variables and member functions in my class ClassA are static. If a user is trying (by mistake) to create an object of this class, he receives a warning: "ClassA, local variable never referenced", because all the functions are static, so this object is never referenced. So, I want to prevent the user from trying to create...

Why must const members be intialized in the constructor initializer rather than in its body?

Why must class members declared as const be initialized in the constructor initializer list rather than in the constructor body? What is the difference between the two? ...

Do you need to unit test a constructor.

Do you need to unit test constructors, say I have a ctor like this, IMapinfoWrapper wrapper; public SystemInfo(IMapinfoWrapper mapinfoWrapper) { this.wrapper = mapinfoWrapper; } Do I need to write a unit test for this ctor? I don't have any getters for the wrapper variable, so I don't need t...

Need I to put overload or override words after the constructor declaration in derived class?

I have a class hierarchy, this one: type TMatrix = class protected //... public constructor Create(Rows, Cols: Byte); //... type TMinMatrix = class(TMatrix) private procedure Allocate; procedure DeAllocate; public constructor Create(Rows, Cols: Byte); constructor CreateCopy(var t...