constructor

JavaScript: What's wrong with this constructor?

I'm not entirely sure how to implement objects in JS. Here is a constructor: function FooList(arg1, arg2, arg3, arg4, arg5, arg6, arg7) { alert("constructing"); this._arg1 = arg1; this._arg2 = arg2; this.refresh(); } I am trying to call it here: FOO_LIST = new FooList( arg1, arg2, arg3, arg4, arg...

Is calling base class constructor always necessary in C++?

Suppose I have some class C, and I inherit from it and name this class D. Do I always have to call C's default constructor as in this example: class C { public: C() { ... } }; class D { public: D() : C() { ... } }; Note that C has only the default constructor. Do I have to call it from D? I couldn't figure out...

What do I do if constructor fails to allocate memory in C++?

I just came across a problem where the constructor of a class needs to allocate memory. So I happily wrote char *mem = static_cast<char*>(malloc(100*sizeof(*mem)));. But then I suddenly realized that in case of error I can't return error code (I am not using exceptions in my code). How can I solve this problem? Should I add an bool init...

.NET Structure Concept Problem

Why does constructor not required in structure ? Why does GC don't remove structures ? ...

Dynamic memory and constructor exceptions

Early today I discovered function try-catch blocks (from here in fact) and then went on a bit of a research spree - apparently they're main use is it catch exceptions throw in by a constructor initialiser list. Anyway, this sent me thinking about failing constructors and I've got to a stage where I just need a little clarification. This...

NSObject default constructor

Hi all, The default constructor of NSObject is this? -(id)init { return self; } thanks! ...

Constructor of derived class with base instance as parameter

I have this code: #include <stdio.h> class A { public: A() { printf("A::A()\n"); } A(const A &a) { printf("A::A(A &a)\n"); } A &operator=(const A &a) { printf("A::operator=\n"); } }; class B : public A { public: B() { printf("B:B()\n"); } B(const A &a) : A(a) { printf("B::B(A &a)\n"); } B &operator=(const B &b)...

C++/CLI, static constructor outside class declaration

Hello, How do I put body of static constructor of a managed class outside class declaration? This syntax seems to be compilable, but does it really mean static constructor, or just a static (=not visible outside translation unit) function? ref class Foo { static Foo(); } static Foo::Foo() {} ...

Object instantiation using eval in JavaScript

Short question, if this works (and it does): eval("new " + generator.className + "(" + generator.constructorArgs.join(", ") + ")"); why doesn't this work: eval(generator.className + ".prototype.constructor.apply({}, generator.constructorArgs);"); The second expression always returns undefined, but in my opinion it should work. I ...

Creating an easy to maintain copy constructor

Hi, Consider the following class: class A { char *p; int a, b, c, d; public: A(const &A); }; In the above I have to define a copy constructor in order to do a deep copy of "p". This has two issues: most of the fields should simply copied. Copying them one by one is ugly and error prone. the more important problem is that whene...

How do I register type with Unity where constructor parameter is HttpSessionStateBase?

I've got interface IImportManager with SessionImportManager that implements this interface. SessionImportManager's constructor must be passed an instance of HttpSessionStateBase in order to work properly. How should I register IImportManager with Unity? I've tried _unityContainer.RegisterInstance<IImportManager>(new SessionImportManag...

C++: References as constructor arguments, help...

Hi, I have a base class(Base) whose constructor takes a reference as argument. In my derived class its constructor, I call the superclass-constructor and of course I need to pass a reference as argument. But I have to obtain that argument from a method of which the return type is by value... I will give a short example: class Base { p...

Construct an ArrayList of strings in Java as simple as one can in Javascript

In JavaScript I can build an Array of string values like: var stuff = new Array('foo','bar','baz','boz','gaz','goz'); or even easier var stuff = 'foo,bar,baz,boz,gaz,goz'.split(','); In Java it seems overly verbose and complex... is there an easier way than this? ArrayList<String> stuff = new ArrayList<String>(); stuff.add("foo");...

C++ basic constructor question

Hello! How should I handle the following situation : I am writing my own 2D vector class and have the following code: class Vector2 : public (...) public: Vector2(float x, float y) { local_vector_storage_[0] = x; local_vector_storage_[1] = y; } template <typename Iterator> Vector2(Iterator begin, Iterator end) ...

Why can't the first parameter list of a class be implicit?

scala> class A(implicit a: Int); defined class A scala> class B()(implicit a: Int); defined class B scala> new A()(1) res1: A = A@159d450 scala> new B()(1) res2: B = B@171f735 scala> new A(1) <console>:7: error: too many arguments for constructor A: ()(implicit a: Int)A new A(1) Why does Scalac insert an empty parameter list...

Why is BaseController's overloaded constructor not being executed?

I have a base controller which is defined as follows, but the constructor that takes the ISiteService never gets executed: public class BaseController : Controller { private ISiteService _siteService; public BaseController() {} public BaseController(ISiteService siteService) { _siteService = siteService; // thi...

Is it acceptable having parameter in class constructor?

Guys for wc, a rubygem I'm writing and that is useful for counting word occurrences in a text, I choose to put 3 parameters in class constructor. The code is working, but I want to refactor it for niceness. In your experience, it's easier to read/mantain/use as API a class with a constructor with no params and a lot of setters/getters m...

Why does my C++ subclass need an explicit constructor?

I have a base class that declares and defines a constructor, but for some reason my publicly derived class is not seeing that constructor, and I therefore have to explicitly declare a forwarding constructor in the derived class: class WireCount0 { protected: int m; public: WireCount0(const int& rhs) { m = rhs; } }; class WireCo...

Java: One constructor or method that will accept array or set or list or ...?

In Java is there anyway to have one constructor that will accept an array or a collection? I have been fiddling with this for a while but I don't think it is possible. I would like to be able to initialize MyClass, like this: MyClass c = new MyClass({"rat", "Dog", "Cat"}); And like this: LinkedList <String> l = new <String> Link...

Problem with Order of "Registration" of .NET Classes in a Messaging Scenario

I've seen this problem come up a lot, but never adequately handled, and I haven't seen it on Stack Overflow, so here goes. I wish there were a way to put this shortly and succinctly without lacking clarity, but I can't seem to shorten it, so bear with me... A good case-study (my current case, of course) to illustrate the problem follow...