constructor

constructors and destructors - c++

Hi, I need to write a program that prints 100 stars on the screen (at random places), and then the stars disappear slowly - one after another. I'm not allowed to use loops nor recursions. I've tried to play with the constructors and the destructors but I can't get the stars to disappear one after another (and not all together). Any idea...

Constructing a (somewhat) complex object

When I create classes, simple constructors tend to be the norm. On one of my current projects, a movie library, I have a Movie domain object. It has a number of properties, resulting in a constructor as follows: public Movie(string title, int year, Genre genre, int length, IEnumerable<string> actors) { _title = title; _year = ye...

How do I force a variable to be initialized in a JavaFX object?

I am making a javaFX class and I need one of the variables to be initialized in order for it to work (in my program there's no default value I can use). This is the best I've come up with, but I'd like something that wont compile unless you initialize the variable. Example Class: Public class Class1{ public-init var var1:String; ...

Why can't the C# constructor infer type?

EDIT: updated the question after PostMan pointed out the error on my part Just out of curiosity, does anybody know why type inference is not supported for constructor the way they are for generic methods? i.e. public class MyType<T> { private readonly T field; public MyType(T value) { field = value; } } var obj = new MyType(42);...

Instantiate a class object with constructor that accepts a string parameter?

I'm having a problem of instantiating a class object, what I want to do is to instantiate the object with constructor accepting "String", here is the code: Object object = null; Class classDefinition = Class.forName("javax.swing.JLabel"); object = classDefinition.newInstance(); it instantiate the JLabel object without a text, i wa...

C++ Singleton design question.

I have a requirement to have a only a single instance of a class at any given point of time. Singleton is the obvious candidate. But I have some other conditions that are not typical of a Singleton. The lifetime of the singleton is not the lifetime of the program. This object has to be created every time I enter a particular state and ...

Dynamically load assemly in static constructor

I have read the following code: public class DalFactory { private static IDataContext _instance = null; static DalFactory() { string asm = ConfigurationManager.AppSettings["DAL-Assembly"]; string cls = ConfigurationManager.AppSettings["DAL-Type"]; Assembly a = Assembly.L...

Unity Application Block - Constructor injection in configuration file

How can I specify that constructor with no parameter should be used while creating the object? I know how to do it for the parameterized one but cannot find any help for the parameter less constructor. I know how to do this through code but need solution for doing it through configuration. ...

C++ Too many destructors called so few objects

Here is the code (also at http://pastebin.com/yw5z2hnG ): #include <iostream> #include <vector> using namespace std; class X { public: int i; X(); ~X(); }; X::X() { i = 1; cout << "---constructor" << '\n'; } X::~X() { cout << "***desctructor" << '\n'; } int main() { vector<X> *vx = new vector<X>; ...

How can I disable external inheritance (without classes) in C#?

How can I disable external inheritance without using classes? I know it can be done with classes like this: public abstract class NoInherit { internal NoInherit() { } } public sealed class MyType : NoInherit { /* ... */ } But not with interfaces. I don't want to use classes because I want to inherit MyType from another class and ...

"Call to undefined method" when trying to pass handle as a constructor argument

I have a user class that I need to use my database with. To do this, I am passing the database handle as a constructor argument like so: index.php: <?php include('classes/user.class.php'); $db = new mysqli('localhost', 'root', '', 'testdb'); if ($mysqli->connect_error) { die('Database connection failed (' . $mysqli->connect_errno...

C++ string reassigned, is the old string correctly freed?

I have a c++ class with a member that is a string, something like: class Phone { string name; void foo() { name = string("new_name"); } } Now, within the function "foo", I reassign the string to "new_name". My question is: What happens to the old, empty string? Is it correctly "freed"? Does it still occupy memory? Now I ini...

How Do I Call An Inherited JavaScript Constructor With Parameters?

Greetings, After reading the following article I have a question: https://developer.mozilla.org/en/Introduction_to_Object-Oriented_JavaScript In the inheritance example, the Person constructor doesn't take any parameters. How would this same example look if I were to add one and call it from the Student constructor? Thanks! ...

Side effect / Volatile / Copy Constructor / Destructor

With reference to the discussion here $3.7.1/2 - "If an object of static storage duration has initialization or a destructor with side effects, it shall not be eliminated even if it appears to be unused, except that a class object or its copy may be eliminated as specified in 12.8." $12.8/15- "When certain criteria are met, an implemen...

Constructing objects in __init__

I've seen code that looks something like this: class MyClass: def __init__(self, someargs): myObj = OtherClass() myDict = {} ...code to setup myObj, myDict... self.myObj = myObj self.myDict = myDict My first thought when I saw this was: Why not just use self.myObj and self.myDict in the begi...

Is generic constructor in non-generic class supported?

Is it not supported, is it supported but I have to do some tricks? Example: class Foo { public Foo<T1,T2>(Func<T1,T2> f1,Func<T2,T1> f2) { ... } } the generics are only used in constructor, there is no field/property depended on them, I use it (generics) to enforce the type correlation for f1 and f2. Remark: I found the w...

Why do we need static private array to initialize other non static private array fields?

If you take a look inside Stack<T> class from .NET 4.0, you will notice that there is an "emptyArray" private static field which is used inside constructors to initialize a real "array" private field. private T[] array; private static T[] emptyArray; private int size; private int version; static Stack() { Stack<T>.emptyArray = new ...

Initializing default values for private fields in constructors explicitely.. WTF?

Consider the following code snippet from .NET 4.0 library: private T[] array; private static T[] emptyArray; private int size; private int version; static Stack() { Stack<T>.emptyArray = new T[0]; } public Stack() { array = Stack<T>.emptyArray; size = 0; version = 0; } Is there any reason behind initiali...

python: return value from __new__

EDIT I actually called object.__new__(cls), and I didn't realize that by this I built an object of class cls! Thanks for pointing this out to me. ORIGINAL QUESTION The documentation says If new() does not return an instance of cls, then the new instance’s init() method will not be invoked. However, when I return object.__...

python: get constructor to return an existing object instead of a new one

I have a class that knows its existing instances. Sometimes I want the class constructor to return an existing object instead of creating a new one. class X: def __new__(cls, arg): i = f(arg) if i: return X._registry[i] else: return object.__new__(cls) # more stuff here (such as __...