constructor

In there a generic constructor with parameter constraint in C#

In C# you can put a constraint on a generic method like: public class A { public static void <T> Method (T a) where T : new() { //...do something... } } Is there also a way to put a constraint like "there exists a constructor with a float[,] parameter?" The following code doesn't compile well: public class A { ...

How to set a default parameter for a vector <string> for use in a default constructor within a class?

For example, a class named Table, with its constructor being: Table(string name="", vector <string> mods); How would I initialize the vector to be empty? Edit: Forgot to mention this was C++. ...

Value checking logic inside or outside of a class ?

Take this skeleton class that someone wants to fill in to fetch RSS streams on a series of web sites: public class RSSStream extends Thread { public RSSStream(String rssStreamName,String rssURL,int refreshTime){ // constructor code goes here } } Now, let's consider that refreshTime has to be higher than zero and that rss...

Java: Abstract class constructors and this()

I feel like I'm missing something here; can someone point out what I'm misunderstanding?! I've got two classes, an Abstract and a Concrete, as follows: public abstract class Abstract { protected static int ORDER = 1; public static void main (String[] args) { Concrete c = new Concrete("Hello"); } public Abs...

Static block in Ruby

Hi. I have been a Java programmer for a while and I am trying to switch to ruby for a while. I was just trying to develop a small test program in ruby and my intention is something like following. I want to create a simple linked list type of an object in ruby; where an instance variable in class points to another instance of same typ...

How can I define a parameterless constructor in a partial LINQ-to-SQL class?

I have a LINQ-to-SQL class called Task. I also have a partial class called Task which adds methods to its functionality: public partial class Task : Item {... However, now I want to execute code when a Task object is constructed but if I had a constructor: public Task() { ... } it tells me: 'TestApp.Models.Task' already d...

constructor and variable names in c++ vs java

Hi All, I'm learning c++ coming from a java background (knowing a little C from many years ago)... in Java, it's common practice to use "this" inside a constructor to distinguish the variable passed in as arguments to the constructor from the one declared in the class: class Blabla { private int a; private int b; Blabla(i...

C# constructor execution order

In C#, when you do Class(Type param1, Type param2):base(param1) is the constructor of the class executed first, and then the superclass constructor is called or does it call the base constructor first? ...

Loading an object from PHP session, does it call constructor?

If I save some object in a php session variable $_SESSION['geoip'] = new GeoIP(); and then on future page loads I use $geoip = $_SESSION['geoip'], will it call the constructor? My constructor for that GeoIP class is making a call to an remote API (CURL), so I was hoping to save on API calls by only doing it the first time and then stori...

PostSharp: OnMethodBoundaryAspect on derived class's constructor

Hi I have an aspect that writes something to the console on exception. I have a base class that throws an exception on its constructor, and a derived class that have the aspect on its constructor. I would expect that the derived class aspect on constructor will catch the base class exception, but it dont. Is this by design? Is this a ...

union members may not have constructors, but `std::pair` okay?

union members may not have destructors or constructors. So I can't template the following class Foo on my own MyClass if MyClass has a constructor: template<class T> struct Foo { T val; Foo(T val_) : val(val_) {} size_t hash() const { union {T f; size_t s;} u = { val }; return u.s; } }; struct MyClass { bool a; doubl...

Can I construct a JavaScript object without using the new keyword?

Here is what I'd like to do: function a() { // ... } function b() { // Some magic, return a new object. } var c = b(); c instanceof b // -> true c instanceof a // -> true b instanceof a // -> true Is it possible? I can make b be an instance of a easily by hooking a into its prototype chain but then I have to do new b(), which is...

In Scala, how do you define a local parameter in the primary constructor of a class?

In Scala, how does one define a local parameter in the primary constructor of a class that is not a data member and that, for example, serves only to initialize a data member in the base class? For example, in the following code, how could I properly define parameter b in the primary constructor of class B so that it generates only a te...

Scala compiler error due to constructor parameter (property) having same name in both base and derived class and used in derived method

Short of renaming the constructor parameter in the primary constructor of class B, what change can I make to the following code (without changing its function) so that Scala will compile it successfully? Example: class A(var a: Int) class B(a: Int) extends A(a) { def inc(value: Int) { this.a += value } } Error: $ scala construct.s...

Is it better to use Enumerable<T>.Empty() as opposed to new List<T> to initialize an IEnumerable?

Suppose you have a class Person : public class Person { public string Name { get; set;} public IEnumerable<Role> Roles {get; set;} } I should obviously instanciate the Roles in the constructor. Now, I used to do it with a List like this : public Person() { Roles = new List<Role>(); } But I discovered this static method i...

Constructor with an array of subclasses of an abstract class as a parameter

I'm working on a player inventory system for a game. I have a struct Slot which has a List<Loot> collection that represents which kinds of items are allowed in it. The abstract class Loot is subclassed by all items which are lootable - i.e.: would be valid Content values for the Slot struct. I want to express that a Slot can have res...

Can constructor call another constructor in c++?

class A{ A(int a = 5){ DoSomething(); A(); } A(){...} } Can the first constructor call the second one? ...

PHP[OOP] - How to call class constructor manually?

Please see the code bellow: 01. class Test { 02. public function __construct($param1, $param2, $param3) { 03. echo $param1.$param2.$param3; 04. } 05. } 06. 07. $params = array('p1','p2','p3'); 08. 09. $ob = new Test; 10. 11. if(method_exists($ob,'__construct')) { 12. call_user_func_array(array($ob,'__construct'),$...

C++ template specialization of constructor

I have a templated class A<T, int> and two typedefs A<string, 20> and A<string, 30>. How do I override the constructor for A<string, 20> ? The following does not work: template <typename T, int M> class A; typedef A<std::string, 20> one_type; typedef A<std::string, 30> second_type; template <typename T, int M> class A { public: A(...

No appropriate default constructor available?

I've got a peculiar error writing some C++/CLI code. I'm trying to make a copy of a class which holds some data. The class is defined as: public ref class RawDataPacket { protected: float* m_internalData; public: RawDataPacket(const float* newInternalData); RawDataPacket(const RawDataPacket^ rdp); RawDataPacket(RawDataPacket^ rdp)...