constructor

Accessing constructor of an anonymous class

Lets say I have a concrete class Class1 and I am creating an anonymous class out of it. Object a = new Class1(){ void someNewMethod(){ } }; Now is there any way I could overload the constructor of this anonymous class. Like shown below Object a = new Class1(){ void someNewMethod(){ } publ...

Why does the c# compiler emit Activator.CreateInstance when calling new in with a generic type with a new() constraint?

When you have code like the following: static T GenericConstruct<T>() where T : new() { return new T(); } The C# compiler insists on emitting a call to Activator.CreateInstance, which is considerably slower than a native constructor. I have the following workaround: public static class ParameterlessConstructor<T> where T :...

JavaScript - detect if function called as constructor

Given a function: function x(arg) { return 30; } You can call it two ways: result = x(4); result = new x(4); The first returns 30, the second returns an object. How can you detect which way the function was called inside the function itself? Whatever your solution is, it must work with the following invocation as well: var Z = n...

Why should you not use Number as a constructor?

I entered this statement in JSLint: var number = new Number(3); And received the following message: Do not use Number as a constructor. Why is that? The statement is creating a number object, not a primitive value, so I don't see why using new is a problem. EDIT: Thanks for all the responses. They've got me thinking further, so...

Why in JavaScript is a function considered both a constructor and an object?

I have been doing a lot of research on this lately, but have yet to get a really good solid answer. I read somewhere that a new Function() object is created when the JavaScript engine comes across a function statement, which would lead me to believe it could be a child of an object (thus becoming one). So I emailed Douglas Crockford, and...

Are varargs allowed in a java enum constructor?

enum MyEnum { A( 1, 2, 3, 4), B(1, 2), C(4, 5, 8, 8, 9); private MyEnum( int firstInt, int... otherInts ) { // do something with arguments, perhaps initialize a List } } Are there any problems with this? Any reasons not to do it? ...

How to pass method result as parameter to base class constructor in C++?

I've trying to achieve something like this: class Base { public: Base(string S) { ... }; } class Derived: Base { public: int foo; string bar() { return stringof(foo); // actually, something more complex }; Derived(int f) : foo(f), Base(bar()) { }; } Now, this doesn't work as I want, because bar() is ca...

asp.net mvc - static constructor

i want ask some question about asp.net mvc Is static constructor will init every user request? Is static data share for every user? ...

Can WCF Service have constructors?

When I new a WCF service in my solution, can I do the following, have a constructor with parameter to pass in? If yes, how, when and where does the runtime fill in my required IBusinessLogic object? [ServiceContract] public interface IServiceContract { [OperationContract] ... } public class MyService : IServiceContract { I...

JavaScript: correct prototype chain for Function

What is the correct output (meaning correct by the ECMA standard) of the following program? function nl(x) { document.write(x + "<br>"); } nl(Function.prototype); nl(Function.prototype.prototype); nl(Function.prototype.prototype == Object.prototype); nl(Function.prototype.prototype.prototype); Chrome and IE6 agree in saying: functio...

overloading constructors and reusing code

Let's say I've got an object Customer with a couple properties (ID, FirstName, LastName). I've got the default constructor Customer(), but then I've also got a Customer(DataRow dr), since I load this object from a database and that's a simple way to do it. I frequently come to a point where I want to set up another constructor, Custome...

Generics: What's a "CONSTRUCTOR constraint"?

I made a custom TObjectList descendant designed to hold subclasses of a base object class. It looks something like this: interface TMyDataList<T: TBaseDatafile> = class(TObjectList<TBaseDatafile>) public constructor Create; procedure upload(db: TDataSet); end; implementation constructor TMyDataList<T>.Create; beg...

C++ Abstract class - constructor issues

Socket has a constructor that takes a winsock SOCKET as parameter and stores it in a private variable: Socket::Socket(SOCKET s) { this->s = s; } I'm trying to make an abstract class "GameSocket" that will parse data from my Socket class: class GameSocket : public Socket { protected: void ParseData(unsigned char* data, int s...

About constructors/destructors and new/delete operators in C++ for custom objects.

Suppose I have a Linked List I created myself. It has its own destructor, which frees the memory. This Linked List does not overload new or delete. Now, I'm trying to create an array of said linked lists (open hashing, if I understand correctly). Then I allocate the necessary memory inside the constructor of this open hashing class. The...

Instantiating objects with a Configuration class or with Parameters

I am running into a design disagreement with a co-worker and would like people's opinion on object constructor design. In brief, which object construction method would you prefer and why? public class myClass { Application m_App; public myClass(ApplicationObject app) { m_App = app; ...

Why might one also use a blank constructor?

I was reading some Java recently and came across something (an idiom?) new to me: in the program, classes with multiple constructors would also always include a blank constructor. For example: public class Genotype { private boolean bits[]; private int rating; private int length; private Random random; public Genotype() { ...

Convention for prototype inheritance in JavaScript

I see a lot of code like this: function Base() {} function Sub() {} Sub.prototype = new Base(); However, if you do: s = new Sub(); print(s.constructor == Sub); This is false. This seems confusing to me, since s's constructor is, indeed, Sub. Is it conventional/better to do this? function Base() {} function Sub() {} Sub.prototype =...

Benefits of using a constructor?

Hi Guys, In my quest in trying to learn more about OOP in PHP. I have come across the constructor function a good few times and simply can't ignore it anymore. In my understanding, the constructor is called upon the moment I create an object, is this correct? But why would I need to create this constructor if I can use "normal" functio...

C++ Copy Constructors

Hi all, I recently wrote a piece of code which did SomeClass someObject; mysqlpp::StoreQueryResult result = someObject.getResult(); where SomeClass::getResult() looks like: mysqlpp::StoreQueryResult SomeClass::getResult() { mysqlpp::StoreQueryResult res = ...<something>...; return res; } Now, using the example in the first code sn...

Can I use templates instead of macros for Exception class creation?

I often want to define new 'Exception' classes, but need to have an appropriate constructor defined because constructors aren't inherited. class MyException : public Exception { public: MyException (const UString Msg) : Exception(Msg) { }; } Typedefs don't work for this, because they are simply aliases, not new classes. Curre...