constructor

static method or instance constructor

In a language were both are available, would you prefer to see an instance constructor or a static method that returns an instance? For example, if you're creating a string from a char[]: String.FromCharacters(chars) new String(chars) ...

Why is Class.newInstance() "evil"?

Ryan Delucchi asked here in comment #3 to Tom Hawtin's answer: why is Class.newInstance() "evil"? this in response to the code sample: // Avoid Class.newInstance, for it is evil. Constructor<? extends Runnable> ctor = runClass.getConstructor(); Runnable doRun = ctor.newInstance(); so, why is it Evil? ...

C++ Constructor

I'm reading this C++ open source code and I came to a constructor but I don't get it ( basically because I don't know C++ :P ) I understand C and Java very well. TransparentObject::TransparentObject( int w, int x, int y, int z ) : _someMethod( 0 ), _someOtherMethod( 0 ), _someOtherOtherMethod( 0 ), _someMethodX( ...

Question about shallow copy in C++

Say I have a struct "s" with an int pointer member variable "i". I allocate memory on the heap for i in the default constructor of s. Later in some other part of the code I pass an instance of s by value to some function. Am I doing a shallow copy here? Assume I didn't implement any copy constructors or assignment operators or anythi...

Difference initializing static variable inline or in static constructor in C#

I would like to know what is the difference between initializing a static member inline as in: class Foo { private static Bar bar_ = new Bar(); } or initializing it inside the static constructor as in: class Foo { static Foo() { bar_ = new Bar(); } private static Bar bar_; } ...

Can initialization list in constructors be used in template classes?

I find that most books concerning C++ templates don't tell anything about whether it's possible or not to use initialization list in constructor of a template class. For example, I have code like this: template <class T> class Stack { T* data; std::size_t count; std::size_t capacity; enum {INIT = 5}; public: Stack()...

Can I use Class.newInstance() with constructor arguments?

I would like to use Class.newInstance() but the class I am instantiating does not have a nullary constructor. Therefore I need to be able to pass in constructor arguments. Is there a way to do this? ...

How can a class have no constructor?

Hi, A while back I asked about instantiating a HttpContext object. Now that I have learnt what I didn't know, what confuses me is that you cannot say HttpContext ctx = new HttpContext(); because the object does not have a constructor. But doesn't every class need a constructor? In C#, if you don't provide one, the compiler automaticall...

Class Constructor never executed in release mode.

Exactly what the title says. I'm using MSVC++ 2008 express, and my class constructor is never executed when compiled in release mode. It DOES work in debug mode. I am doing something like: ClassTest test; test.DoIt(); Breakpoints on DoIt(); trigger, but breakpoints on ClassTest::ClassTest(); do not. ...

C++: ctors for structs?

C++: Since a struct is a class with everything "public", are default -ctors created and called? The reason I ask is to understand the overhead, if any, that C++ may have over C, when structs are used. An opinion I have heard is that classes have some overhead that structs don't, in C++, but I question this. ...

Should members of an object be automatically set in the class?

When you have a complex property, should you instantiate it or leave it to the user to instantiate it? For example (C#) A) class Xyz{ List<String> Names {get; set;} } When I try to use, I have to set it. ... Xyz xyz = new Xyz(); xyz.Name = new List<String>(); xyz.Name.Add("foo"); ... Where as if I modify the code B) cla...

How to find amount of parameters in a constructor.

I'm trying to find a way to determine how many parameters a constructor has. Now I've built one constructor with no parameters and 1 constructor with 4 parameters. Is there, in C#, a way to find out how many parameters a used or given constructor has? Thing is, I'm using a third constructor to read log files. These logs files are read...

Abstract class constructor in Java

Can an abstract class have a constructor? If so, how it can be used and for what purposes? ...

Can I use identical names for fields and constructor parameters?

class C { T a; public: C(T a): a(a) {;} }; Is it legal? ...

There's a way to declare Copy Constructor non-public AND using default copy Constructor?

I have a not-so-small class under development (that it changes often) and I need not to provide a public copy constructor and copy assignment. The class has objects with value semantics, so default copy and assignment work. the class is in a hierarchy, with virtual methods, so I provide a virtual Clone() to avoid slicing and to perform ...

Is it considered bad design to do lengthy operations in a constructor?

I am implementing a class to compare directory trees (in C#). At first I implemented the actual comparison in the class's constructor. Like this: DirectoryComparer c = new DirectoryComparer("C:\\Dir1", "C:\\Dir2"); But it doesn't feel "right" to do a possible lengthy operation in the constructor. An alternative way is to make the cons...

If abstract base C++ class is actually an interface, so it own no data members, is it obligatory to call base class constructor in derived class constructor?

I have a code: class AbstractQuery { virtual bool isCanBeExecuted()=0; public: AbstractQuery() {} virtual bool Execute()=0; }; class DropTableQuery: public AbstractQuery { vector< std::pair< string, string> > QueryContent; QueryValidate qv; public: explicit DropTableQuery(const string& qr): AbstractQuery(), qv(q...

How can you initialize a class with a reference member from a private constructor?

I'm creating an interface wrapper for a class. The member within the class is a reference(to avoid copying the large structure). If I create a private constructor, what is the best way to initialize that reference to appease the compiler? struct InterfaceWrapper { InterfaceWrapper( SomeHugeStructure& src ):m_internal(src){}; i...

How do I call one constructor from another in Java?

Is it possible to call a constructor from another (within the same class, not from a subclass)? If yes how? And what could be the best way to call another constructor (if there are several ways to do it)? ...

How much work should be done in a constructor?

Should operations that could take some time be performed in a constructor or should the object be constructed and then initialised later. For example when constructing an object that represents a directory structure should the population of the object and its children be done in the constructor. Clearly, a directory can contain director...