63
голосов
8ответов
21402 просмотров

a constructor as a delegate - is it possible in C#?

I have a class like below: class Foo { public Foo(int x) { ... } } and I need to pass to a certain method a delegate like this: delegate Foo FooGenerator(int x); Is it possible to pass the constructor directly as a FooGenerator value, without having to type: delegate(int x) { return new ...

7
голосов
4ответов
27912 просмотров

Constructors in Python

I need help in writing code for a Python constructor method. This constructor method would take the following three parameters: x, y, angle What is an example of this?

6
голосов
8ответов
12425 просмотров

What is the meaning of leading underscores in a C++ constructor?

OK I am not a very experienced C++ programmer, but I was wondering what is the significance of the underscores in the arguments of the following constructor? class floatCoords { public: floatCoords(float _x, float _y, float _width, float _height) : x(_x), y(_y), width(_width), height(_h...

12
голосов
1ответов
1753 просмотров

Initializer list *argument* evaluation order

So, the C++ standard requires that class members be initialized in the order in which they are declared in the class, rather than the order that they're mentioned in any constructor's initializer list. However, this doesn't imply anything about the order in which the arguments to those initializa...

20
голосов
6ответов
5465 просмотров

How does "this" escape the constructor in Java?

I've heard about this happening in non thread-safe code due to improperly constructed objects but I really don't have the concept down, even after reading about in in Goetz's book. I'd like to solidify my understanding of this code smell as I maybe doing it and not even realize it. Please provide...

14
голосов
4ответов
20822 просмотров

Copy Constructor and default constructor

Do we have to explicitly define a default constructor when we define a copy constructor for a class?? Please give reasons. eg: class A { int i; public: A(A& a) { i = a.i; //Ok this is corrected.... } A() { } //Is this re...

22
голосов
6ответов
56416 просмотров

JavaScript: How to create a new instance of a class without using the new keyword?

I think the following code will make the question clear. // My class var Class = function() { console.log("Constructor"); }; Class.prototype = { method: function() { console.log("Method");} } // Creating an instance with new var object1 = new Class(); object1.method(); console.log("New returned...

36
голосов
3ответов
47810 просмотров

Partial Class Constructors

Is there a way to have a partial class' constructor call another method that my or may not be defined? Basically my partial class constructor is defined: public partial class Test { public Test() { //do stuff } } I would like to be able to somehow insert extra code to...

5
голосов
2ответов
1929 просмотров

Spring Application Context available inside the constructor

I am having an issue when trying to create beans from a spring Application Context inside a bean instantiated by spring using constructor arguments. I have implemented the ApplicationContextAware interface but it populates the context after the instance is created (obvious). But then, if you n...

0
голосов
7ответов
3665 просмотров

When are member data constructors called?

I have a global member data object, defined in a header (for class MyMainObj) like this. class MyMainObj { MyDataObj obj; } MyDataObj has a default constructor. When is the constructor for MyDataObj called? Is it called as part of the creation of MyMainObj?

2
голосов
5ответов
133 просмотров

Modifying state of other objects in a constructor: design no-no?

I'm refactoring some code and found this (simplified of course, but general idea): class Variable: def __init__(self): self.__constraints = [] def addConstraint(self, c): self.__constraints.append(c) class Constraint: def __init__(self, variables): for v in ...

38
голосов
3ответов
17416 просмотров

Is there a way to initialize an object through a hash?

If I have this class: class A attr_accessor :b,:c,:d end and this code: a = A.new h = {"b"=>10,"c"=>20,"d"=>30} is it possible to initialize the object directly from the hash, without me needing to go over each pair and call instance_variable_set? Something like: a = A.new(h) ...

54
голосов
3ответов
17101 просмотров

How to make the class constructor private in Ruby?

class A private def initialize puts "wtf?" end end A.new #still works and calls initialize and class A private def self.new super.new end end doesn't work altogether So what's the correct way? I want to make new private and call it via a factory method.

0
голосов
1ответов
915 просмотров

Creating a default image in a UserControl

I have a User Control that has one child - an Image. I'm trying to set a default image using code in the User Control's default constructor to display an image resource, but so far with no success in either Blend preview or when I actually use it in a running app. I do not get any errors either. ...

1
голосов
6ответов
142 просмотров

When should variables be set in a class

Hay, when should variables be set within a PHP class? <?php class MyClass { var $my_var; // here? like var $my_var = null; function __construct(){ $this->my_var = null; // or here? } } ?>

216
голосов
15ответов
246876 просмотров

How do I get a PHP class constructor to call its parent's parent's constructor?

I need to have a class constructor in PHP call its parent's parent's (grandparent?) constructor without calling the parent constructor. // main class that everything inherits class Grandpa { public function __construct() { } } class Papa extends Grandpa { public function __co...

0
голосов
3ответов
252 просмотров

Issue with JavaScript object constructor where arguments are other objects

I'm writing some JavaScript with three classes, one for Roofs, one for Garages, and one for Houses. The house class takes two arguments to its constructor, a Roof and a Garage. When I run this code I get: can not construct object [Break on this error] throw new Error('can not construct object');...

2
голосов
2ответов
5421 просмотров

JavaScript constructor parameter types

I have a JavaScript class representing a car, which is constructed using two parameters, which represent the make and model of the car: function Car(make, model) { this.getMake = function( ) { return make; } this.getModel = function( ) { return model; } } Is there a way to verify tha...

3
голосов
3ответов
1994 просмотров

Is there something like PostConstruct for JAXB-annnotated classes?

Is there such a functionality in JAXB to perform operations on a class after it is unmarshalled i.e. after it is constructed by JAXB? If not, how could I achieve this?

5
голосов
5ответов
5281 просмотров

Two ways of calling default constructor

I have the following code: struct B { //B() {} int x; int y; }; void print(const B &b) { std::cout<<"x:"<<b.x<<std::endl; std::cout<<"y:"<<b.y<<std::endl; std::cout<<"--------"<<std::endl; } int main() { B b1 = B(); //init1 B b2; //...

8
голосов
7ответов
1565 просмотров

When does it make more sense to use the factory pattern rather than an overloaded constructor to instantiate an object?

In Karl Seguin's Foundations of Programming there is a small section on using the factory pattern. He closes the passage by stating "you can accomplish the same functionality with constructor overloading", but doesn't indicate when or why? So,when does it make more sense to use the factory patt...

4
голосов
3ответов
1818 просмотров

C++ Initialization list and memory alloc

Is the following valid? class myClass { private: ... int m_nDataLength; boost::shared_array<int> m_pData; ... public: myClass(): ..., m_nDataLength(10), m_pData(new int[m_nDataLength]), ... { } } Am I right in assuming that the initialization wil...

201
голосов
10ответов
128910 просмотров

difference between variables inside and outside of __init__()

Is there any difference at all between these classes besides the name? class WithClass (): def __init__(self): self.value = "Bob" def my_func(self): print(self.value) class WithoutClass (): value = "Bob" def my_func(self): print(self.value) Does it mak...

13
голосов
6ответов
4864 просмотров

Is it bad form to call the default assignment operator from the copy constructor?

Consider a class of which copies need to be made. The vast majority of the data elements in the copy must strictly reflect the original, however there are select few elements whose state is not to be preserved and need to be reinitialized. Is it bad form to call a default assignment operator fr...

0
голосов
2ответов
654 просмотров

A better way to construct a datetime with higher precision than milliseconds

I have just gone through an unexpectedly convoluted process to define datetimes. The underlying data has a better precision than milliseconds. I ended up constructing an intermediate datetime to the nearest second, reading it's value in ticks (10 000 to the millisecond), adjusting the ticks the...

5
голосов
3ответов
4676 просмотров

Initializing temporary aggregate object using curly braces

Let's say I have a class: class Aggregate { public: int x; int y; }; I know how to initialize an object using curly braces: Aggregate a1 = { 1500, 2900 }; But I can't find a proper syntax to create temporary object and pass it as an argument to some method, for example: void fro...

5
голосов
10ответов
315 просмотров

To init or to construct

I'm reviewing some code and I'm seeing a lot of this: class Foo { public: Foo() { // 'nuffin } void init() { // actual construction code } } ; The only advantage I can see is if you create a Foo without using a pointer and you want to hold off its construction code until l...

3
голосов
5ответов
1876 просмотров

Chain-overloading constructors?

I'm trying to build a class which will initalise its self either by passing in a reference to a record in a database (with the intention that a query will be run against the database and the returned values for the objects properties will be set therein), or by specifying the values "by hand" - t...

4
голосов
4ответов
1984 просмотров

What happens inside the code of Constructor that compiler executes and supplies Default Constructor?

I wanted to What is the Job of Compiler Supplied Constructor ?. Is that constructor does memory allocation and all the stuffs required to create an object. I am not asking this question from member variable initialization point of view. I want to know what happens inside the code of default cons...

1
голосов
6ответов
1380 просмотров

how to know which constructor was used in creating an object?

Consider Class A has two constructors new A(int), new A(int, String) also it has a method show() Then given a statement like, A a1= new A(4); A a2= new A(3, "foo"); and later in code (or in some methods where these object were passed) a1.show(); a2.show(); new A(3).show(); and new A(...