constructor

Passing dependent objects to a parent constructor in Scala

Suppose I have the following class heirarchy: class A() class B(a:A) class C(b:B) class BaseClass(b:B, c:C) Now I want to implement a subclass of BaseClass, which is given an instance of A, and constructs instances of B and C, which it passes to its superclass constructor. If I could use arbitrary expressions, I'd do something like ...

Visibility of reintroduced constructor

I have reintroduced the form constructor in a base form, but if I override the original constructor in a descendant form, the reintroduced constructor is no longer visible. type TfrmA = class(TForm) private FWndParent: HWnd; public constructor Create(AOwner: TComponent; const AWndParent: Hwnd); reintroduce; overload; virtu...

Static and default constuctor

A non static class can have static as well as default constructor at the same time. What is the difference between these two constructors? When shall I go for only static or static with default constructor? ...

Constructors taking references in C++

I'm trying to create constructor taking reference to an object. After creating object using reference I need to prints field values of both objects. Then I must delete first object, and once again show values of fields of both objects. My class Person looks like this : class Person { char* name; int age; public: Person()...

Can a constructor return a NULL value?

I know constructors don't "return" anything but for instance if I call CMyClass *object = new CMyClass() is there any way to make object to be NULL if the constructor fails? In my case I have some images that have to be loaded and if the file reading fails I'd like it to return null. Is there any way to do that? Thanks in advance. ...

In Java, can a final field be initialized from a constructor helper?

I have a final non-static member: private final HashMap<String,String> myMap; I would like to initialize it using a method called by the constructor. Since myMap is final, my "helper" method is unable to initialize it directly. Of course I have options: I could implement the myMap initialization code directly in the constructor. MyC...

Static initialization of a struct with class members

I have a struct that's defined with a large number of vanilla char* pointers, but also an object member. When I try to statically initialize such a struct, I get a compiler error. typedef struct { const char* pszA; // ... snip ... const char* pszZ; SomeObject obj; } example_struct; // I only want to assign the first f...

Can the template parameters of a constructor be explicitly specified?

A constructor of a class can be a template function. At the point where such a constructor is called, the compiler usually looks at the arguments given to the constructor and determines the used template parameters from them. Is there also some syntax to specify the template parameters explicitly? A contrived example: struct A { tem...

Is there anything wrong with taking immediate actions in constructors?

I have classes like this one: class SomeObject { public function __construct($param1, $param2) { $this->process($param1, $param2); } ... } So I can instantly "call" it as some sort of global function just like new SomeObject($arg1, $arg2); which has the benefits of staying concise, being easy to understand,...

Java constructor and modify the object properties at runtime

Note: This is an assignment Hi, I have the following class/constructor. import java.io.*; class Set { public int numberOfElements = 0; public String[] setElements = new String[5]; public int maxNumberOfElements = 5; // constructor for our Set class public Set(int numberOfE, int setE, int maxN...

ensuring a well-defined object state when a constructor throws an exception

I have a Visual Studio 2008 C# .NET 2.0CF application. I'm using a component base from which two concrete components are derived. The application first attempts to use SomeDisposableComponent. Its constructor throws an exception because it requires a feature that isn't available. Then, the application tries SomeOtherDisposableComponent. ...

C++ method chaining including class constructor

Hello, I'm trying to implement method chaining in C++, which turns out to be quite easy if the constructor call of a class is a separate statement, e.g: Foo foo; foo.bar().baz(); But as soon as the constructor call becomes part of the method chain, the compiler complains about expecting ";" in place of "." immediately after the cons...

C++ class is not recognizing string data type

I'm working on a program from my C++ textbook, and this this the first time I've really run into trouble. I just can't seem to see what is wrong here. Visual Studio is telling me Error: identifier "string" is undefined. I separated the program into three files. A header file for the class specification, a .cpp file for the class impl...

UserControl Constructor with parameters

My Problem is when I want to use UserControl with parameter. it call both constructor( constructor with no parameter And constroctor with parameters) is this normal situation?? if not, how should I construct the object. public partial class FreeExperience : Arche.Web.UI.UserControlBase { private ItemInfo itemInfo; public F...

Accessing the constructor by using Reflection

Assume the class is public and and the constructor is internal like as Public class A { private string text; internal A(string submittedText); public string StrText { get; } } In this case how could I Access the constructor by using Reflection. What I have done so far Type[] pTypes = new Type[1]; pTypes[0] = typeof(obj...

initializing a vector of custom class in c++

Hey basically Im trying to store a "solution" and create a vector of these. The problem I'm having is with initialization. Heres my class for reference class Solution { private: // boost::thread m_Thread; int itt_found; int dim; pfn_fitness f; double value; std::vector<double> x; public: ...

Constructor with non-instance variable assistant?

I have a number of classes that look like this: class Foo(val:BasicData) extends Bar(val) { val helper = new Helper(val) val derived1 = helper.getDerived1Value() val derived2 = helper.getDerived2Value() } ...except that I don't want to hold onto an instance of "helper" beyond the end of the constructor. In Java, I'd do so...

Preventing call of a private constructor from within the class in java

Hi We can restrict the creation of object of a class by making its constructor private. But this constructor could still be called from within the class. Is there any way to prevent this in Java? Thanks. ...

Python constructor does weird things with optional parameters

Possible Duplicate: least astonishment in python: the mutable default argument Can you help me understand of the behaviour and implications of the python __init__ constructor. It seems like when there is an optional parameter and you try and set an existing object to a new object the optional value of the existing object is p...

Setting up the Business Logic Layer with a DBML

I'm busy on making a Business Logic Layer. Now I had a problem since i want to program using c# and the whole project i have to make a module for is vb.net based. I have solved this using a new project in the solution. This project will maintain the dataclasses and Business Logic. Now i've run to an issue, atleast i don't know what to do...