Questions tagged [multiple-constructors]

Questions regarding object instantiation/injection with multiple constructors

57 questions
400
votes
25 answers

Best way to do multiple constructors in PHP

You can't put two __construct functions with unique argument signatures in a PHP class. I'd like to do this: class Student { protected $id; protected $name; // etc. public function __construct($id){ $this->id = $id; //…
Jannie Theunissen
  • 28,256
  • 21
  • 100
  • 127
60
votes
4 answers

In Scala, how can I subclass a Java class with multiple constructors?

Suppose I have a Java class with multiple constructors: class Base { Base(int arg1) {...}; Base(String arg2) {...}; Base(double arg3) {...}; } How can I extend it in Scala and still provide access to all three of Base's constructors? In…
Seth Tisue
  • 29,985
  • 11
  • 82
  • 149
12
votes
6 answers

Choosing constructor for a noncopyable object

Assume I have a non-copyable class with multiple constructors with like this class Foo: boost::noncopyable { public: Foo(std::string s) {...}; // construct one way Foo(int i) {...}; // construct another way } Now, I want to…
Roddy
  • 66,617
  • 42
  • 165
  • 277
11
votes
4 answers

How to extend ImageView in an Android-Scala app?

I have tried many solutions found in google by the keywords: multiple constructors, scala, inheritance, subclasses. None seems to work for this occasion. ImageView has three constructors: ImageView(context) ImageView(context,attribute…
George Pligoropoulos
  • 2,919
  • 3
  • 33
  • 65
11
votes
5 answers

How to simplify multiple constructors?

I would like to have two constructors for a class, as follows: public MyClass() { // do stuff here } public MyClass(int num) { MyClass(); // do other stuff here } Is the above the correct way to achieve my purpose? Is there some kind…
CJ7
  • 22,579
  • 65
  • 193
  • 321
10
votes
2 answers

Default value for boost::shared_ptr on class constructor

Suppose I have class like class A{ public: A(int a, boost::shared_ptr ptr){ // whatever! } }; My question is, what's the default value for that ptr? I'd like to be able to create an instance of that class using A…
José Tomás Tocino
  • 9,873
  • 5
  • 44
  • 78
8
votes
2 answers

How to implement php constructor that can accept different number of parameters?

How to implement php constructor that can accept different number of parameters? Like class Person { function __construct() { // some fancy implementation } } $a = new Person('John'); $b = new Person('Jane', 'Doe'); $c = new…
hoyomi
  • 277
  • 1
  • 6
  • 19
8
votes
3 answers

Few questions about constructors in C#

In C# regarding the inheritance of constructors: I have read that constructors cannot be inherited. If the base class contains a constructor, one or more, the derived class have to always call one of them? It is not possible that the derived class…
JeffreyZ.
  • 387
  • 1
  • 3
  • 7
8
votes
2 answers

javascript: different constructors for same type of object

is it possible to have more than one constructors for a class in javascript? i.e. one with zero parameters, one with one, one with two, etc... if so, how? thanks!
clamp
  • 33,000
  • 75
  • 203
  • 299
8
votes
2 answers

MEF Constructor Parameters with Multiple Constructors

I'm starting to use MEF, and I have a class with multiple constructors, like this: [Export(typeof(ifoo))] class foo : ifoo { void foo() { ... } [ImportingConstructor] void foo(object par1) { ... } } I am using…
InterWAS
  • 183
  • 1
  • 2
  • 7
7
votes
5 answers

this() and base() constructors in C#

There seems to be no language syntax for specifying both a this() and a base() constructor. Given the following code: public class Bar : Foo { public Bar() :base(1) //:this(0) { } public Bar(int schmalue) :Foo(1) { …
Sprague
  • 1,610
  • 10
  • 22
7
votes
3 answers

Which is better Java programming practice: stacking enums and enum constructors, or subclassing?

Given a finite number of items which differ in kind, is it better to represent them with stacked enums and enum constructors, or to subclass them? Or is there a better approach altogether? To give you some context, in my small RPG program (which…
Arvanem
  • 1,043
  • 12
  • 22
5
votes
6 answers

C++ constructor question

In the C++ programming for the absolute Beginner, 2nd edition book, there was the following statement: HeapPoint::HeapPoint(int x, int y): thePoint(new Point(x,y)) { } Is this equal to: HeapPoint::HeapPoint(int x, int y) { thePoint = new…
Simplicity
  • 47,404
  • 98
  • 256
  • 385
4
votes
3 answers

Scala: Generic class with multiple constructors

I'm trying to create a generic class like this: class A[T](v: Option[T]) { def this(v: T) = this(Some(v)) def this() = this(None) def getV = v } Then I do some testing: scala> new A getV res21: Option[Nothing] = None scala> new A(8)…
Vilius Normantas
  • 3,708
  • 6
  • 25
  • 38
4
votes
2 answers

Inheriting constructors work only partially

I have following class, written so, as to work completely, whatever the typedef is: class A { protected: typedef uchar mDataType; std::vector mData; uint32 mWidth; uint32 mHeight; friend class C; public: A(); …
1
2 3 4