constructor

Use reflection to determine which base class constructor is called

// Using reflection on the type DerivedClassB // can we determine: // a) that it uses the base class ctor that takes a string param // b) the actual value that it passes? public class BaseClass { public BaseClass() { } public BaseClass(string someParameter) { } } public class DerivedClas...

What's wrong with overridable method calls in constructors?

I have a Wicket page class that sets the page title depending on the result of an abstract method. public abstract class BasicPage extends WebPage { public BasicPage() { this.previousPage = previousPage; add(new Label("title", getTitle())); } protected abstract String getTitle();...

Format(line wrapping) constructor initializer list in Eclipse CDT

I tried to find a solution for now ~30min and couldn't find any. I am trying to set up the code style in CDT so it gives me: MyClass::MyClass() : var1(1), var2(2), var3(3){ } instead of MyClass::MyClass() : var1(1), var2(2), var3(3){ } but I couldn't find an option to do so. The only 'initializer list' option I could f...

When move ctor will be invoked?

Given class: class C { public: C() { cout << "Dflt ctor."; } C(C& obj) { cout << "Copy ctor."; } C(C&& obj) { cout << "Move ctor."; } C& operator=(C& obj) { cout << "operator="; return obj; } C& operator=(C&& obj) { cout << "Move oper...

What is the difference between C# & CLI when it comes in with value types and constructors?

I read recently that the C# and CLI standards define different ways to handle value types and constructors. According to the CLI specification value types can't have parameterless constructors, whereas in the C# specification value types have a default parameterless constructor. If, according to the CLI specification, you need to create...

How do I create a C# array using Reflection and only type info?

I can't figure out how to make this work: object x = new Int32[7]; Type t = x.GetType(); // now forget about x, and just use t from here. // attempt1 object y1 = Activator.CreateInstance(t); // fails with exception // attempt2 object y2 = Array.CreateInstance(t, 7); // creates an array of type Int32[][] ! wrong What's the secret ...

Cannot find constructor File() in java.io.File

This is probably obvious, so bear with me. YES, I KNOW THAT java.io.File has no default constructor. The problem is that When I try to extend java.io.File, it says "Cannot find constructor File() in java.io.File" even though I am overriding the default constructor in java.lang.Object. Here is my code: AbsRelFile.java import java....

Why can't access constructor in body of another constructor ?

Hello everybody I know that i can access constructor on another constructor like below but is there a way to access it in body of constructor ? public Rectangle(int size) : this(size, size) { Console.WriteLine("Square Constructor Called"); //this(size,size); i want to access like this } ...

Why is my overloaded C++ constructor not called?

Hi, I have a class like this one: class Test{ public: Test(string value); Test(bool value); }; If I create an object like this: Test test("Just a test..."); The bool constructor is called! Anyone knows why? Thanks ...

a compiler generated default constructor in c++

Hi, declaring a struct Table: struct Tables { int i; int vi[10]; Table t1; Table vt[10]; }; Tables tt; assuming that a user-deault contructor is defined for Table. here tt.t1 will be initialized using the default contructor for Table, as well as each element in tt.vt. On the other hand tt.i and tt.vi are...

Baffling JavaScript Inheritance Behavior

<disclaimer> What follows is the fruits of a thought experiment. What I'm doing isn't the issue; the symptoms are. Thank you. </disclaimer> I've finally wrapped my head around constructors, prototypes, and prototypal inheritance in JavaScript. But the SomethingSpectactular method in the sample below bugs me: function FinalClass() { ...

C++ vectors of classes with constructors.

//Using g++ and ubuntu. #include <vector> using namespace std; Define a class: class foo(){ (...) foo(int arg1, double arg2); } Constructor: foo::foo(int arg1, double arg2){ (...) //arrays whose length depend upon arg1 and arg2 } I would like to do something like this: vector<foo> bar(10); //error: no matching function for cal...

C++ Template Default Constructor

Hi there! I got a little problem with templates: template <typename T> T Func(){ std::string somestr = ""; // somestr = ... if (somestr != ""){ return boost::lexical_cast<T>(somestr); } else{ T ret; // warning: "ret may be uninitialized in this function" return ret; } } If this functio...

Objective C static vs. dynamic constructors

Hi, Is it best to have static constructors where you alloc the instance in the constructor and you return the instance as auto release, e.g. [String stringWithFormat...] or is it best to have dynamic constructors where you ask the user to alloc first so that he is in charge of releasing? When should you use each? Cheers ...

overloaded cast operator or single argument constructor

If a class has a single argument constructor my understanding is it is implicitly convertible by the constructor to the type of the argument in appropriate contexts. Defining a conversion operator also makes a class convertible to another type. Questions Does the conversion operator ever get called implicitly? If both a single argume...

Java: Converting from .NET Constructors

We're converting some .NET 3.5 code to Java (Android). This Java code gives the error: Syntax error on token "Chapters", VariableDeclaratorId expected after this token this.add (new Book() {Chapters=50, OneBasedBookID = 1, Long = "Bahai", Short = "ba", Color = c, BookType = b; }); The types are all correct. ...

Initializing C++ const fields after the constructor

I want to create an immutable data structure which, say, can be initialized from a file. class Image { public: const int width,height; Image(const char *filename) { MetaData md((readDataFromFile(filename))); width = md.width(); // Error! width is const height = md.height(); // Error! height is const } }; Wha...

What is the right way to allocate memory in the C++ constructor?

Which is the right way to allocate memory via new in the C++ constructor. First way in the argument list: class Boda { int *memory; public: Boda(int length) : memory(new int [length]) {} ~Boda() { delete [] memory; } }; or in the body of constructor: class Boda { int *memory; public: Boda(int l...

Constructor injection in application where dependencies might not be able to be initalized

Hi, My application is able to start without a database connection, how do I handle this with IoC and constructor injection? Example: public class ApplicationShellPresenter(IRepository repository, IView view) { } When IRepository in this case will be constructed an exception will be thrown due to underlaying DAL cannot find config/f...

python sub class constructor

In python OOP, lets say, Person is a parent class with its own constructor; then Student is a sub class of Person, before I use Student, must Person.__init__(self) be called first in the constructor of Student? Plus, can I define a new constructor in Student class? class Person(): def __init__(self): Above is class Person ...