constructor

Why does this() and super() have to be the first statement in a constructor?

Java requires that if you call this() or super() in a constructor, it must be the first statement. Why? For example: public class MyClass { public MyClass(int x) {} } public class MySubClass extends MyClass { public MySubClass(int a, int b) { int c = a + b; super(c); // COMPILE ERROR } } The Sun compiler says "call to super ...

(.net) When is an attribute's constructor run?

When is it run? Does it run for each object to which I apply it, or just once? Can it do anything, or its actions are restricted? ...

Having a problem with placement-new!

I am having a problem placing an instance of my reference-counting Pointer<Type> class into my Array class. Using the debugger, it seems that the constructor is never called (which messes up the reference-count and causes a segfault down the line)! My push_back function is: void push_back(const T& element) { if (length >= max) reall...

Error when using a Python constructor

class fileDetails : def __init__(self,host,usr,pwd,database): self.host=host self.usr.usr self.pwd=pwd self.database=database def __init__(self,connection,sql,path): self.connection=mysql_connection() self.sql=sql self.path=path If I use the constructor then it gives an ...

CodeContracts: How to fullfill Require in Ctor using this() call?

I'm playing around with Microsoft's CodeContracts and encountered a problem I was unable to solve. I've got a class with two constructors: public Foo (public float f) { Contracts.Require(f > 0); } public Foo (int i) : this ((float)i) {} The example is simplified. I don't know how to check the second constructor's f for being >...

Basic examples of single-inheritence super() in Python

Let's say I have the following classes set up: class Foo: def __init__(self, frob, frotz): self.frobnicate = frob self.frotz = frotz class Bar: def __init__(self, frob, frizzle): self.frobnicate = frob self.frotz = 34 self.frazzle = frizzle How can I (if I can at all) use sup...

How to refactor a class with overloaded constructors

I have a class with overloaded constructor (C#) It can be initialized in few ways, and some parameters are optional - so in result - there is a confusing bunch of constructors new Object(StrA, StrB, ObjA) new Object(StrA, StgB, ObjB, StrC) new Object(StrA, StrB, ObjA, StrD) new Object(StrA, StrB, ObjB, StrC, StrD) new Object(StrA, StrB,...

Constructor overloading in Java - best practice

There are a few topics similar to this, but I couldn't find one with a sufficient answer. I would like to know what is the best practice for constructor overloading in Java. I already have my own thoughts on the subject, but I'd like to hear more advice. I'm referring to both constructor overloading in a simple class and constructor ov...

Java: What is the difference between these methods of construction.

What's the difference between these two methods of initializing the observers ArrayList. Or any other type for that matter. Is one faster than the other? Or am I missing some other benefit here. class Publisher implements Observerable { private ArrayList observers = new ArrayList(); } class Publisher implements Observerable { ...

How much work should the constructor for an HTML parsing class do?

How much work is it reasonable for an object constructor to do? Should it simply initialize fields and not actually perform any operations on data, or is it okay to have it perform some analysis? Background: I was writing a class which is responsible for parsing an HTML page and returning various information based on the parsed informat...

Initializing a lot of fields - constructor vs method return values

Hi, I'm currently working on a Java trading card game, similar to the old Pokémon one. What I now want to do is to define all the cards in some way, but because there's a lot of fields that need to be initialized, I'm thinking of alternative ways, because a constructor will be very long and hardly readable for each card. I also have to i...

Why doesn't __attribute__((constructor)) work in a static library?

In the following example, the program should print "foo called": // foo.c #include <stdio.h> __attribute__((constructor)) void foo() { printf("foo called\n"); } // main.c int main() { return 0; } If the program is compiled like this, it works: gcc -o test main.c foo.c However, if foo.c is compiled into a static library, t...

Get TypeInfo in static constructor.

Is there any way to get the equivalent of GetType within a static constructor? I want to iterate through the available properties of the type within the static constructor but GetType is an instance method? Why is this? The typeinfo should exist in the static context. Is there a way around this?? ~ Cameron ...

Structs With Constraints

I've had a hard time figuring out how I can write a struct in C# that has constraints on its fields' values. For example, System.DateTime DateTime d = new DateTime(); puts the value 01/01/0001 12:00:00 AM in d. But I can't write an explicit parameterless constructor, as structs are not allowed to have an explicit parameterless const...

C++ Lite Question 10.19. Function instead of variable decl

I had this problem happen to me in the past http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.19 My question is, when writing Foo x(Bar()); Why is it "declaring a non-member function that returns a Bar object" ? i can understand it if i wrote Foo x(Bar); But what does it think the () means in Bar()? ...

Calling PHP Parent Constructors With Old/New Syntax.

Given the class Foo with an old-style constructor class Foo { public function Foo() { //does constructing stuff } } Is there any functional difference between calling the parent constructor with a new style constructor or the old style constructor? class Bar extends Foo { public function Bar() { //does it matter? //parent:...

How to use base class's constructors and assignment operator in C++?

I have class B with a set of constructors and an assignment operator. class B { public: B(); B(const string & s); B(const B & b){(*this) = b;}; B & operator= (const B & b); private: virtual void foo(); // and other private member variables and functions } I want to create an inheriting class D that will just override the...

How to use a doctrine construct function

Doctrine documentation says you can use public function construct() { ... } as a construct function since __construct can't be overridden. When I place it in my code and put a echo in it public function construct() { echo "constructing..."; } it isn't called during the construction of the object. How is it supposed to be called o...

Manual invocation of constructor?

Suppose I am allocating an arbitrary block of memory. Part of this block is atomic data (ints, bytes, etc.) and some of this block of data I want to be occupied by objects. Can I turn any arbitrary piece of memory into an object through a constructor call, such as data->MyObject () and subsequently destroying the object via data->~MyOb...

What's wrong with my Shared Add-in Constructor?

Good morning, fellows developers: I'm currently trying to fix several performance issues of an Excel Shared Add-in inherited from a previous developer, basically I'm trying to find how the add-in stuff works internally in Excel, meaning I had searched the net for information an my understanding is: In the registry the LoadBehaviour sh...