constructor

C++ methods overload in python

Hi, suppose a C++ class has several constructors which are overloaded according the number and type and sequences of their respective parameters, for example, constructor(int x, int y) and constructor(float x, float y, float z), I think these two are overloaded methods, which one to use depends on the parameters, right? So then in python...

PHP - extended __construct

Hi guys, i was wonderinf if you could help me out.. I have two classes, one extends the other.. Class B will be extended by various different objects and used for common database interactions.. Now i would like class B to handle its connect and disconnects without direction from class A or any external input.. The problem from what i ...

Call the constructor of the unknown children

Hi! I want to call the constructor; class anAbstractClass { public: anAbstractClass(inputdatatype){/*blablabla*/} }; class aChield : public anAbstactClass { /* ... */ } void _engine::initShader(_anAbstractClass** inShader) { *inShader = new /*???*/(inputdata for the construcor) } aChield* theChield; _engine* myEngine = new _engi...

passing parameters from constructor to functions in processing/java

I'm having trouble with some objects in processing. the code should have two objects displayed and moving. but i only see one object displayed and moving. maybe there's something i'm missing. check out the code. Rule myRule; Rule myRule1; void setup() { size(200,200); smooth(); //Initialize rule objects myRule = new Rule(0,100...

Nested templates and constructor

Hi everybody! Edit: Note that my final purpose here is not having the class working, is just learning more about templates :-) Suppose you have a template class which implements a vector: template <typename T> class Vector { public: Vector(size_t dim) { dimension = dim; elements = new T[dim]; ...

Inheritance in c# question

I have some rudimentary question about inheritance in C#. I have two classes. I want the child class to inherit all members of the base class (e.g. x and y), but it seems to use inheritance I should initialize the base constructor. If I define another x and y in child class so that i later initialize the base constructor so what is the ...

Should we always have a zero-argument constructor in a Class?

Should every Java class have a zero-argument constructor? ...

HTML in Mootools' Element constructor?

I'm currently using the Mootools Element constructor method to dynamically add a new row into a table. function newPermission(somedata) { var newUserPerm = new Element('tr', { 'html': '<td>foo</td><td>bar</td>' }); newUserPerm.inject('permissions_table'); } However, upon checking the resulting code, the following H...

C++: calling a virtual base class's overloaded constructor

Hi, is there a (practicable) way to by-pass the normal (virtual) constructor calling order? (Hard to describe in one sentence, see the) Example: class A { const int i; public: A() : i(0) { cout << "calling A()" << endl; } A(int p) : i(p) { cout << "calling A(int)" << endl; } }; class B : public...

how to create a null string?

I have two constructors MyObj(String s){ //first constructor ... if(s==null) s = somecode; this.s = s; ... } MyObj(): this(null) { } //second constructor In this way, if the empty constructor is called, it will redirect to the first constructor and initialise the value as determined by some code. However, now...

Declare an object in C++ w/o creating it?

Is this possible? For example if i write Car myCar; Then the constructor taking no arguments of Car is called. It results in an error if there is only a constructor taking arguments. In Java I can easily declare an object and create it later using the exact same statement as above. ...

How does BinaryFormatter.Deserialize create new objects?

When BinaryFormatter deserializes a stream into objects, it appears to create new objects without calling constructors. How is it doing this? And why? Is there anything else in .NET that does this? Here's a demo: [Serializable] public class Car { public static int constructionCount = 0; public Car() { construction...

Can we have a return type for a constructor in Java?

The following code gives a compilation error: class parent { parent(int a){} } class child extends parent{} Error: Main.java:6: cannot find symbol symbol : constructor parent() location: class parent class child extends parent{} ^ 1 error I was trying to do different things and found that adding a return type to the parent cons...

surprising constructors for a class!

Hi, What is the problem here in this code? It gives segmentation fault. I found value of size in vector (int *a) is no more 3. How is this? #include <iostream> using namespace std; class vector { int *v; int size; public: vector(int m) { v = new int[size = m]; for(int i=0; i<size; i++) v[i] = ...

: this() As a constructor

I'm trying to get a better understanding of general practice... specifically deriving this() in a constructor. I understand that its less code, but I consider it less readable. Is it common/good practice to do it this way? Or is it better to write a second constructor that handles it specifically? public SomeOtherStuff(string rabble)...

Overriding constructors in Java

Hi, For school I need to learn Java and since I'm used to C++ (like Cocoa/Objective-C) based languages, I get really frustrated on Java. I've made a super-class (that can also be used as a base-class): public class CellView { public CellViewHelper helper; // CellViewHelper is just an example public CellView() { this.h...

Using make_shared with a protected constructor + abstract interface.

Given an abstract interface and an implementation derived from that interface, where constructors are protected (creation of these objects only being available from a class factory - to implement a DI pattern), how can I make use of make_shared in the factory function? For example: class IInterface { public: virtual void Me...

How to throw an exception from an enum constructor?

How can I throw an exception from an enum constructor? eg: public enum RLoader { INSTANCE; private RLoader() throws IOException { .... } } produces the error Unhandled exception type IOException ...

Early return from a Scala constructor

I am writing the constructor for my "main" class. The first thing it does is call a method to use commons-cli to parse the command line. If the parseOptions method returns false, an error has occurred, and the constructor should exit. I tried writing the following code if (!parseOptions(args)) return but the compiler complains that I...

Can we have a body for a default constructor in C# during runtime?

Hi everyone!In a class there will be a constructor.If a programmer defines that then definitely it will have a body. But if we don't define it then will that constructor will have a default body in it? ...