constructor

What is the name of term meaning next approach: A a = new A { Prop1 = a, Prop2 = b };

Hi! Could you please tip how to properly name object construction approach where public proprieties values are being set on object creation? For example, SqlCommand command = new SqlCommand { Connection = connection, CommandType = CommandType.Text }; ...

Why some object doesn't have constructor in IE?

Below javascript has different effect in different browsers. document.write(this.location.constructor); document.write("<br/>"); document.write(this.constructor); document.write("<br/>"); In Chrome, the page has function Location() { [native code] } function DOMWindow() { [native code] } In Firefox, the page has [object Location]...

Inheritance in Java - creating an object of the subclass invokes also the constructor of the superclass. Why exactly?

Hallo, I have a question about inheritance in Java. I have two classes A and B, and class B, inherits from A: public class A { public A() { System.out.println("Hi!"); } } public class B extends A { public B() { System.out.println("Bye!"); } public static void main(String[] args) { ...

How many constructors should a class have?

I'm currently modifying a class that has 9 different constructors. Now overall I believe this class is very poorly designed... so I'm wondering if it is poor design for a class to have so many constructors. A problem has arisen because I recently added two constructors to this class in an attempt to refactor and redesign a class (SomeMa...

C++ virtual function from constructor

Why the following example prints "0" and what must change for it to print "1" as I expected ? #include <iostream> struct base { virtual const int value() const { return 0; } base() { std::cout << value() << std::endl; } virtual ~base() {} }; struct derived : public base { virtual const int value() const { ...

Why can't I create an abstract constructor on an abstract C# class?

I am creating an abstract class. I want each of my derived classes to be forced to implement a specific signature of constructor. As such, I did what I would have done has I wanted to force them to implement a method, I made an abstract one. public abstract class A { abstract A(int a, int b); } However I get a message saying the a...

Strange behavior in constructor

Hi, I have a class made up of several fields, and I have several constructors. I also have a constructor that doesn't take any parameters, but when I try to use it: int main { A a; } The compiler generates an error, while if I use it like this: int main { A a(); } It's ok. What's that? Thank you ...

dojo: inheritance with default value - the mixin doesn't happen...

I wish to declare a new dojo class inheriting from an existing dojo class, but with my own choice of default values for the class's properties. (The user can still override those values.) I am declaring my own version of the dijit.form.FilteringSelect such that: the hasDownArrow property defaults to false (rather than the standard tr...

Destructor called on object when adding it to std::list

I have a Foo object, and a std::list holding instances of it. My problem is that when I add a new instance to the list, it first calls the ctor but then also the dtor. And then the dtor on another instance (according to the this pointer). A single instance is added to the list but since its dtor (along with its parents) is called, the o...

Call a constructor on a already allocated memory

Hello everyone, here is a short question : Using C++, how can I call a constructor on a memory region that is already allocated ? ...

C# - How can I set the value of auto property backing fields in a struct constructor?

Given a struct like this: public struct SomeStruct { public SomeStruct(String stringProperty, Int32 intProperty) { this.StringProperty = stringProperty; this.IntProperty = intProperty; } public String StringProperty { get; set; } public Int32 IntProperty { get; set; } } Of course, a compiler error ...

What things are best not done in a constructor?

I started off by drafting a question: "What is the best way to perform unit testing on a constructor (e.g., __construct() in PHP5)", but when reading through the related questions, I saw several comments that seemed to suggest that setting member variables or performing any complicated operations in the constructor are no-nos. The const...

Using the F# pipe symbol with an object constructor

I'm trying to figure out the correct syntax to use the pipe operator |> into the creation of an object. Currently I'm using a static member to create the object and just piping to that. Here is the simplified version. type Shape = val points : Vector[] new (points) = { points = points; } static member create(poi...

How much code should one put in a constructors (Java)?

Hello, I was thinking how much code one should put in constructors in Java? I mean, very often you make helper methods, which you invoke in a constructor, but sometimes there are some longer initialisation things, for example for a program, which reads from a file, or user interfaces, or other programs, in which you don't initialise onl...

Can I invoke the base constructor of a class from calling code?

I have two classes, one derived from the other and both have parametrized constructors. I want to call the constructor in both classes when I instantiate the derived class. So my question is: what is the syntax to pass parameters to both base and derived classes from the calling code? I tried something like this but it does not compile...

Constructor in C

Is there a way to have some kind of default constructor (like C++ one) for C user types defined with a structure? I already have a macro which works like fast initializer (like pthread_mutex's one) but i wanted to know if you can by any chance have some (or all) fields of a struct filled at declaration. For instance with the pthread_mu...

Force explicit initialization of members in a c# constructor

Is there a way to cause an error if every constructor doesn't initialize a given member? Maybe something with const? I want this because I have a pile of code where I'm getting errors as a result of incorrectly computing member values. What I'd really like to do is strip out the whole mess and follow the compiler's lead while redoing it...

Named constructor and inheritance

I'm working on C++ framework and would like to apply automatic memory management to a number of core classes. So far, I have the standard approach which is class Foo { public: static shared_ptr<Foo> init() { return shared_ptr<Foo>(new Foo); } ~Foo() { } protected: Foo() { } }; // Example of use shared_...

prototype and constructor object properties

I've: function Obj1(param) { this.test1 = param || 1; } function Obj2(param, par) { this.test2 = param; } now when I do: Obj2.prototype = new Obj1(44); var obj = new Obj2(55); alert(obj.constructor) I have: function Obj1(param) { this.test1 = param || 1; } but the constructor function has been Obj2... why that? O...

InitializeComponent in both constructors, or in one with constructor inheritance?

Is there any practical difference in terms of effects on the component model between: class MyComponent : Component { public MyComponent() { InitializeComponent(); } public MyComponent(IContainer container) { container.Add(this); InitializeComponent(); } } and: class MyComponent : Component { ...