Questions tagged [instantiation]

Instantiation is the process of creating objects from a class in most object oriented and object based languages. In the C++ language, instantiation is the process of creating a class or function from a class template or function template.

2609 questions
90
votes
4 answers

How to instantiate class from name string in Rails?

How we can instantiate class from it's name string in Ruby-on-Rails? For example we have it's name in database in format like "ClassName" or "my_super_class_name". How we can create object from it? Solution: Was looking for it myself, but not found,…
89
votes
11 answers

Declaring an object before initializing it in c++

Is it possible to declare a variable in c++ without instantiating it? I want to do something like this: Animal a; if( happyDay() ) a( "puppies" ); //constructor call else a( "toads" ); Basially, I just want to declare a outside of the…
Quantum7
  • 3,165
  • 3
  • 34
  • 45
87
votes
7 answers

How is an instance initializer different from a constructor?

In other words, why would you need an instance initializer? What difference or advantage do you have in writing a instance initializer over a constructor?
Ajay
  • 7,378
  • 18
  • 57
  • 75
84
votes
9 answers

How to create inline objects with properties?

In Javascript it would be: var newObject = { 'propertyName' : 'propertyValue' }; newObject.propertyName; // returns "propertyValue" But the same syntax in Python would create a dictionary, and that's not what I want new_object = {'propertyName':…
Jader Dias
  • 88,211
  • 155
  • 421
  • 625
83
votes
6 answers

Is there a way to instantiate a class without calling __init__?

Is there a way to circumvent the constructor __init__ of a class in python? Example: class A(object): def __init__(self): print "FAILURE" def Print(self): print "YEHAA" Now I would like to create an instance of A. It…
Woltan
  • 13,723
  • 15
  • 78
  • 104
76
votes
8 answers

What's the difference between dict() and {}?

So let's say I wanna make a dictionary. We'll call it d. But there are multiple ways to initialize a dictionary in Python! For example, I could do this: d = {'hash': 'bang', 'slash': 'dot'} Or I could do this: d = dict(hash='bang', slash='dot') Or…
verix
  • 1,703
  • 2
  • 15
  • 12
76
votes
2 answers

PHP class instantiation. To use or not to use the parentheses?

I've always assumed that - in the absence of constructor parameters - the parentheses (curly brackets) follow the class name when creating a class instance, were optional, and that you could include or exclude them at your own personal whim. That…
Atli
  • 7,855
  • 2
  • 30
  • 43
62
votes
7 answers

Does { } act like ( ) when creating a new object in C#?

I just noticed that using {} instead of () gives the same results when constructing an object. class Customer { public string name; public string ID {get; set;} } static void Main() { Customer c1= new Customer{}; //Is this a…
user4451265
60
votes
6 answers

Why do I get an error instantiating an interface?

I have a class and an interface, and when I try to instantiate the interface, I get an error: Cannot create an instance of the abstract class or interface My code is below: namespace MyNamespace { public interface IUser { int…
Alex
  • 39,265
  • 21
  • 45
  • 42
60
votes
6 answers

How do I force a particular instance of a C++ template to instantiate?

See title. I have a template. I want to force a particular instance of a template to instantiate. How do I do this? More specifically, can you force an abstract template class to instantiate? I might elaborate as I have the same question. In my…
anon
  • 41,035
  • 53
  • 197
  • 293
48
votes
7 answers

How does one instantiate an array of maps in Java?

I can declare an array of maps using generics to specify the map type: private Map[] myMaps; However, I can't figure out how to instantiate it properly: myMaps = new HashMap[count]; // gives "generic array…
Karl von L
  • 1,591
  • 3
  • 15
  • 22
47
votes
4 answers

In PHP 5 can I instantiate a class dynamically?

Is it possible to dynamically instantiate a class using a variable? For example is something like this possible in PHP? class foo { public $something; } $class_name = "foo"; $f = new $class_name();
Bijou Trouvaille
  • 8,794
  • 4
  • 39
  • 42
46
votes
2 answers

Can't Instantiate Map...well why not?

Map>> k = new Map>>(); This line is in my code. I'd like to instantiate a Map that contains a String then an ArrayList of Pairs of Strings and Integers. Pair is a…
PinkElephantsOnParade
  • 6,452
  • 12
  • 53
  • 91
45
votes
3 answers

Instantiate a class from its textual name

Don't ask me why but I need to do the following: string ClassName = "SomeClassName"; object o = MagicallyCreateInstance("SomeClassName"); I want to know how many ways there are to do this is and which approach to use in which…
Raheel Khan
  • 14,205
  • 13
  • 80
  • 168
44
votes
5 answers

Determine if a type is static

Let's say I have a Type called type. I want to determine if I can do this with my type (without actually doing this to each type): If type is System.Windows.Point then I could do this: Point point1 = new Point(); However if type is…
Beaker
  • 2,804
  • 5
  • 33
  • 54