constructor

Using constructor without operator 'new'

Please help me to understand why the following code works: <script> var re = RegExp('\\ba\\b') ; alert(re.test('a')) ; alert(re.test('ab')) ; </script> In the first line there is no new operator. As far as I know, a contructor in JavaScript is a function that initialize objects created by the operator new and they are n...

How to construct a container of MyClass, where MyClass constructor can throw?

I have something like: #include "MyImage.hpp" // MyImage wraps the Qt library image class namespace fs = boost::filesystem; class ImageCollection { public: ImageCollection(const char* path); private: const fs::path path_; deque<MyImage> instanceDeque_; } ImageCollection(const char* path) : path_(fs::is_directory(path) ? ...

Constructors accepting string reference. Bad idea?

It's considered a bad idea/bad design, have a class with a constructor accepting a reference, like the following? class Compiler { public: Compiler( const std::string& fileName ); ~Compiler(); //etc private: const std::string& m_CurrentFileName; }; or should I use values? I actually do care about performance. Thank you in adva...

C++ design question

Suppose I have a class Base which has a member variable A* my_hash. I also have class Extended which inherits from class Base. I also have a class B which extends A. class Base{ Base(): my_hash(new A) {} //methods which use my_hash protected: A* my_hash; }; class Extended:public Base{ //methods which use my_hash from A //I can...

implicit constructor conversion works on explicit vector::vector, only sometimes

I like to initialize 2-dimensional arrays as vector<vector<int> >(x,y). x is passed to vector<vector<int> >'s constructor and y is passed to vector<int>'s constructor, x times. Although this seems to be forbidden by C++03, because the constructor is explicit, it always works, even on Comeau. I can also call vector::assign like this. But,...

Why does my Java code stop responding after an object declaration?

Hi. I've been working on a text-based RPG in java, just for fun. After spending many hours of tedious work writing almost a dozen classes for weapons, spells, cmbat systems, shopping systems, etc. I wrote a simple class to start and run the game. All it really does is display a main menu, and create an object which in turn creates every...

C++ Array Constructor

hi together, I was just wondering, whether an array member of a class could be created immediately upon class construction: class C { public: C(int a) : i(a) {} private: int i; }; class D { public: D() : a(5, 8) {} D(int m, int n) : a(m,n) {} private: C a[2]; }; As far as...

Constructor: Full fledged or minimal?

When designing classes you usually have to decide between: providing a "full" constructor that takes the initial values for all required fields as arguments: clumsy to use but guarantees fully initialized and valid objects providing just a "default" constructor and accessors for all necessary fields: might be convenient sometimes but d...

Error when using constructor for a generic list that takes a collection

I was trying out parsing XML using Fluent interface as described here. The example is good but gives one compiler error that I am not able to fix. The error is in the protected constructor below: protected DynamicXml( IEnumerable<XElement> elements ) { _elements = new List<XElement>( elements ); // error on this line } The two c...

Passing arguments to constructors using CreateObject in Coldfusion

Hi there, I'm trying to use a Java object in Coldfusion using the CreateObject function. This works fine when the constructor in the Java class doesn't take any arguments e.g.: MyObject myObject = new MyObject(); Goes to myObject = CreateObject("java", "com.something.MyObject"); But I'm not sure how to pass arguments to the cons...

How can I check the failure in constructor() without using exceptions ?

All of the classes that I'm working on have Create()/Destroy() ( or Initialize()/Finalized() ) methods. The return value of the Create() method is bool like below. bool MyClass::Create(...); So I can check whether initialization of the instance is successful or not from the return value. Without Create()/Destroy() I can do the same j...

C++ Class Delegation Constructor Problems

Thank you for reading. The to delegate Class is called Sensor. It need a reference to be set in the Constructor like: class Sensor { Sensor(other *ref);} I have a Class testFactory. If i now type class testFactor{ ...stuff... private: Sensor mySensor;} I get ALL the Problems. It cannot alloc an abstract Object. Or it cannot decla...

javascript apply on constructor, throwing "malformed formal parameter"

thanks to wonderful responses to this question I understand how to call javascript functions with varargs. now I'm looking to use apply with a constructor I found some interesting information on this post. but my code is throwing errors attempt 1: var mid_parser = new Parser.apply(null, mid_patterns); error: TypeError: Function.p...

If the only thing that changes is the constructor should I still derive?

I have a class that has everything already implemented but its initialization process is different for every child class. Is there a better idiom to replace the ctor? Is there something more generic/dynamic that I should use? ...

[C#] Delegate Calling in a Constructor

I've run into an easily solved but a design problem I've not encountered in my young life yet. I have a class that needs to go through a few setup procedures before anything else happens. However, during the construction of this class, I have in the constructor's parameters a delegate that can be passed so that the user can add their o...

How can I make an object in my parent class but bless it into my child class in Perl?

I have two classes: a base class, Foo::Base and a derived class, Foo::Base::Sub. I want to have Foo::Base::Sub do some type and data checking on the constructor`s argument--a hash--before blessing it. I've tried overriding Foo::Base->new's constructor, doing the checks and then calling Foo::Base->new (since the code would be exactly th...

Removing the need for "new"

A nasty gotcha in javascript is forgetting to call new on a function meant to be instantiated, leading to this being bound to a different object (usually the global) instead of a fresh one. One workaround I read about is to check for it explicitly in the function-constructor using the following idiom: function SomeConstructor(x, y, ...)...

What values can a constructor return to avoid returning this?

What are the exact circumstances for which a return statement in Javascript can return a value other than this when a constructor is invoked using the new keyword? Example: function Foo () { return something; } var foo = new Foo (); If I'm not mistaken, if something is a non-function primitive, this will be returned. Otherwise som...

Why are there 2 constructors in the Default AccountController provided by the MVC ?

Hello, here's the default AccountController.cs that's generated by the framework. public class AccountController : Controller { public IFormsAuthentication FormsAuth { get; private set; } public IMembershipService MembershipService { get; private set; } public AccountController() : this(null, null) { } ...

C# memberwisecopy and descendants

I think there's good no answer but before I throw in the towel on doing this gracefully I'll ask here: I have a parent class which holds a bunch of data. These are used only to instantiate a descendant which takes the parent, modifies it and produces the final result. I see how to use MemberwiseCopy to make another instance of the par...