constructor

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 then creating the datetime that I actually wa...

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 from the copy constructor? The default assi...

C++ Initialization list and memory alloc.

Hi, 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 will happen exactly in the order I've given in ...

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 pattern rather than an overloaded constructor ...

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; //init2 print(b1); print(b2); return 0; } When I start program (vs2008, debug) I have the...

Is there something like PostConstruct for JAXB-annnotated classes?

I need to perform certain operations on a class after it was unmarshalled (after it is constructed by JAXB, rather then by myself). Is there such a functionality in JAXB? If not, how could I achieve it? ...

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 that the make and model supplied to the const...

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');\n in Firebug even though the objects are...

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 __construct() { // call Grandpa's ...

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? } } ?> ...

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. Shouldn't this be possible? Here is the X...

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. ...

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) which should cause each instance variable to be ini...

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 variables: v.addConstraint(sel...

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? ...

Spring Application Context available inside the constructor

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

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 be run after the class constructor is cal...

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 required if we write the above copy constructor??...

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 code in your explanation to make it stick...

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 initializations are evaluated. I'm working with a sy...