constructor

PHP constructor to return a NULL.

Just a short question :) I have this code. I wonder if it is possible for a User object constructor to somehow fail so that $this->LoggedUser is assigned a NULL value - object is freed after constructor returns. $this->LoggedUser = NULL; if ($_SESSION['verbiste_user'] != false) $this->LoggedUser = new User($_SESSION['verbiste_user'])...

How to provide many constructors, but without too many dependencies?

This is a pretty basic C++ design question: I have a class that contains some data which is read-only, once the object is constructed: class Foo { private: class Impl; Impl* impl_; public: int get(int i); // access internal data elements }; Now, I'd like to implement several ways to construct a Foo object and fill it with dat...

Avoid specifying all arguments in a subclass

I have a class: class A(object): def __init__(self,a,b,c,d,e,f,g,...........,x,y,z) #do some init stuff And I have a subclass which needs one extra arg (the last W) class B(A): def __init__(self.a,b,c,d,e,f,g,...........,x,y,z,W) A.__init__(self,a,b,c,d,e,f,g,...........,x,y,z) self.__W=W It seems du...

Java constructor question

How do I write a constructor to change ints to ints or longs or strings....I am making a Memory system and I have code for Memory and a Memory Element (MemEl) and my test code and I am trying to write these constructors: MemEl(int), MemEl(long), MemEl(String) I already have done it for shorts and bytes but I need some help with these. Th...

Commands in WPF, Implementation question

I know I've already asked questions on this, but something that's just confusing. I'm doing a tutorial on WPF MVVM pattern, but it seems I cannot get it right, as in the tutorial, it doesn't go into detail on how to implement the ICommand interface. (not sure how correct it really is?!!) I have the following implementation of ICommand:...

How do I declare a default constructor for sub-class of abstract class?

The following doesn't work for me in Java. Eclipse complains that there is no such constructor. I've added the constructor to the sub-class to get around it, but is there another way to do what I'm trying to do? public abstract class Foo { String mText; public Foo(String text) { mText = text; } } public class Bar...

Segfault Copy Constructor

My code is as follows: void Scene::copy(Scene const & source) { maxnum=source.maxnum; imagelist = new Image*[maxnum]; for(int i=0; i<maxnum; i++) { if(source.imagelist[i] != NULL) { imagelist[i] = new Image; imagelist[i]->xcoord = source.imagelist[i]->xcoord; imagelist[i]->ycoord = source.imagelist[i]->ycoord; ...

Should Java Have an "is null" construct?

To check against null we generally do if (object == null). Instead, would it be nicer to have if (object is null) { // Do something } Or if (object is not null) { // Do something } It would be more intuitive, i.e. reads more like english. EDIT : I got lot of answers. Thanks for the people who up voted, although down votes w...

calling super() from an actionscript constructor with varargs

If a constructor takes its parameters as a vararg (...) it seems to be impossible to create a subclass that will just pass on that vararg to the superclass. There is a related question with fix for this same situation for normal functions: Wrapping a Vararg Method in ActionScipt but I cannot get that to work with a super call. base cla...

Does a type require a default constructor in order to declare an array of it?

I noticed that when you declare an array, the default constructor must be needed. Is that right? Is there any exception? For example, struct Foo{ Foo(int i ) {} }; int main () { Foo f[5]; return 0; } The code above does not compile. ...

PHP object overflow

I'm trying to organize classes in a project in a fashion similar to: $TheRoberts = new family; class family { public function __construct() { $bobby = new father; $cindy = new daughter; } } class father extends family { function PayBills() {} function GoToWork() {} } class daughter extends family { ...

C# Object Constructor Overloads

I'm trying to have an overloaded constructor for a class. I think this should be fairly simple, however I can't seem to make it work. Any ideas? public SaveFile(string location) { // Constructor logic here //TODO: Implement save event. this.Save(location); } public SaveFile() { strin...

OOP functionname acting as __construct when its the same name as the class

Bit long title, comes down to this: <?php class error { public function error($errormsg = false, $line = false) { echo 'errormsg: '.$errormsg.' on line: '.$line; } } ?> When i call this class in another file, with for example: <?php $error = new error; ?> It already execut...

Initializing members with members

This is a problem I come across often. The following examples illustrates it: struct A { int m_SomeNumber; }; struct B { B( A & RequiredObject ); private: A & m_RequiredObject; }; struct C { C( ); private: A m_ObjectA; B m_ObjectB; }; The implementation of the constructor of C looks something like this: C::C...

C# class representing a USB device

I'm writing a wrapper class in C# for a USB device (using P/Invoke to communicate with it). The device needs to be sent an open message before use, and a close method when it's done with. In my constructor I want to try to open the device and return null if that call fails for whatever reason. I'll also write a Dispose() which calls the ...

Can I initialize a derived class with an instance of the base class?

Example: public class MyList<T> : List<T> { public MyList(List<T> list) { this = list; // this is basically what I want, but doesn't work base = list; // this also doesn't work } } Any solutions? Or is what I'm trying to achieve simply a bad idea? Motivation: I want to add a custom function to a List object....

Is there a way to do more "dynamic" data constructors in Haskell?

Is there some Haskell extension that enables the creation of more complex data constructors then GADT? Suppose I wanted to create a data structure that is an ordered list, and have a data constructor similar to (:) that work with lists, with type signature: data MyOrdList a where (>>>) :: (Ord a) -> a -> MyOrdList a -> MyOrdList a ...

Cannot create simple class

Hello, I am trying to create a very simple CUser class in my project, but apparently I am doing something wrong. Here is the code: using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Web; using MySql.Data.MySqlClient; namespace admin.NET.lib { public class CUser { prote...

How to get a constructor function to inherit from a constructor function in Javascript?

Hi all, So I'm learning Javascript and all its' prototype goodness, and I am stumped over the following: Say I have this var Animal = function (a, b, c, d, e, f, g, h, i, j, k , l, m, n){ this.a = a; this.b = b; //...etc... }; var x = new Animal(1,2,3....); Now how do I create a Cat constructor function that inherits from ...

How do I call the no arg constructor after I have allready created an object of the class?

I create an object of the class Rectangle rec = new Rectangle(2,4); Which gives my rectangle and height of 2 and a width of 4. But is there anyway I can call the no arg constructor later on in my code without creating a new object? without doing this: Rectangle rec2 = new Rectangle(); ...