constructor

C# When To Use "This" Keyword

Possible Duplicate: When do you use the this keyword? Hello, I understand that the "This" keyword is used to refer to an instance of the class, however, suppose I have a class called Life, which defines two fields, the person (their name) and their partner(their name): class Life { //Fields private string _person; ...

C++ - construction of an object inside a class

I'm fairly new to C++, and I'm not sure about this one. Have a look at the following example which sums up my current problem. class Foo { //stuff }; class Bar { Foo foo; }; So Bar constains a full Foo object, not just a reference or pointer. Is this object initialized by its default constructor ? Do I need to explicitly call...

What is the preferred way of constructing objects in C#? Constructor parameters or properties?

I was wondering, what is the preferred way to construct a new object in C#? Take a Person class: public class Person { private string name; private int age; //Omitted.. } Should I create it to use: New Person("name", 24); or New Person() { Name = "name", Age = 24 }; Is it just a matter of taste or is there a good...

C++ Passing a Copy of an Object vs. Passing a Pointer to an Object?

Here's a constructor for my Game class: // Construct a Game to be played with player on a copy of the board b. Game(const Board& b, Player* player) { ... } Here's how I'm using the constructor: Player p("Player Name"); Board b(6,3); Game g(b, &p); How does this work? Is b being copied? If I want to save a pointer to player, shou...

Strange (to me) constructor syntax can someone explain what this does?

public class PhotoList : ObservableCollection<ImageFile> { public PhotoList() { } **//this is the line that I dont recognise!!!!!!!!!!** public PhotoList(string path) : this(new DirectoryInfo(path)) { } public PhotoList(DirectoryInfo directory) { _directory = directory; Update(); } public...

Why have a Create method instead of using "new"?

What are the advantages and when is it appropriate to use a static constructor? public class MyClass { protected MyClass() { } public static MyClass Create() { return new MyClass(); } } and then creating an instance of the class via MyClass myClass = MyClass.Create(); as opposed to just having a pub...

Constructor access rules.

Hello, if I compile (under G++) and run the following code it prints "Foo::Foo(int)". However after making copy constructor and assignment operators private, it fails to compile with the following error: "error: ‘Foo::Foo(const Foo&)’ is private". How comes it needs a copy constructor if it only calls standard constructor at runtime? #i...

Implementing Clone() method in base class

Here's a Clone() implementation for my class: MyClass^ Clone(){ return gcnew MyClass(this->member1, this->member2); } Now I have about 10 classes derived from MyClass. The implementation is the same in each case. Owing to the fact that I need to call gcnew with the actual class name in each case, I am required to creat...

Is it OK to pass DataReaders to constructors?

I'm maintaining some C# 2.0 code and the programmer uses a pattern of reading a collection of business objects by opening a datareader and then passing it to the object's constructor. I can't see anything obviously wrong with this but it feels bad to me. Is this OK to do? private static void GetObjects() { List<MyObject> objects = ...

Should Constructor initialize own parameters directly to the private member or via the public field ( C# centric)?

E.g. which way is better class Foo { private string _Bar ; Foo ( string bar) { _Bar = bar ; } public string Bar { get { return _Bar ; //more logic here } set { _Bar = value ; //more logic could be added } } } OR class Foo { private string _Bar ; Foo ( string bar) { this.Bar = bar ; } publ...

Javascript: prototype inheritance

Hello guys, I am new to JavaScript OOP. Can you please explain me what the difference is between the following blocks of code. I tested and both blocks work. What's the best practice and why? First block: function Car(name){ this.Name = name; } Car.prototype.Drive = function(){ document.write("My name is " + this.Name + " and...

Advantage using an aggregate initialization list over a constructor?

Hello, I'm new to C++ and I have a question... I tried answering the question myself by making a test application... in debug, the class B initialization generates less assembly code, but in release mode, I can't really say... it optimizes the initializations away :( Let's say I have two classes: class A { public: int a, b, c, d;...

cffunction Access=

If I use access="remote" for binding a cfselect to a cfc, then I lose the ability to have an Init() constructor. <cfselect name="CityID" bind="cfc:Components.City.View1({StateID})" value="CityID" display="CityName" bindonload="yes" /> I'm used to passing the datasource name to the Init function when I instantiate a component, like so:...

Chain-calling parent constructors in python

Consider this - a base class A, class B inheriting from A, class C inheriting from B. What is a generic way to call a parent class constructor in a constructor? If this still sounds too vague, here's some code. class A(object): def __init__(self): print "Constructor A was called" class B(A): def __init__(self): ...

Initializing an object with references without accessing a non-default constructor

Let's suppose I have an interface named "Controller". Several classes implement this interface and I don't know these classes (e.g. the class names are located in an xml-file). Now for this Controller-implementing classes to work they have to get some references to other objects (maybe data objects). And this is my question, namely what ...

Generate HashCode in constructor of immutable type.

Hello, i have some questions about the HashCode of immutable types. May i "pre"-generate the HashCode of an immutable type in the constructor or are there any reason not do that? Should i always generate the Hashcode again, when the method GetHashCode() is called? Here´s a sample class : public class Id { private re...

Python and Qt (PyQt) - calling method before resize event

Hello, i have a question. There is application class in my program. It is inherited from QtGui.QMainWindow. In ini I call my own method which works with graphic. And it should be called before resize event. How can i do that? Thanks. EDIT: As you can se here the value of resize event is 14, and show event is 17. So i should find event w...

Is it possible to exit gracefully in a constructor?

Is it possible to exit gracefully out of a constructor in php? Something to the effect of class Foo { function __construct() { $active = false; if(!$active) { return false; } } } I'm trying to accomplish this because I want to check to see if any of the methods in the class should run based on a configuration ...

What is the default constructor for C++ pointer?

I have code like this: class MapIndex { private: typedef std::map<std::string, MapIndex*> Container; Container mapM; public: void add(std::list<std::tring>& values) { if (values.empty()) // sanity check return; std::string s(*(values.begin())); values.erase(values.begin()); i...

Bad Practice to run code in constructor thats likely to fail?

Hello everyone, my question is rather a design question. In Python, if code in your "constructor" fails, the object ends up not being defined. Thus: someInstance = MyClass("test123") #lets say that constructor throws an exception someInstance.doSomething() # will fail, name someInstance not defined. I do have a situation though, wher...