instantiation

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

眉间皱痕 提交于 2019-11-26 10:28:30
问题 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 case I am building a library, some of the template implementations are large and include lots of stuff, but are only generated for a couple of types. I want to compile them in the library and export all the methods, but not include the header with the

Swift: Creating an Array with a Default Value of distinct object instances

人走茶凉 提交于 2019-11-26 05:54:38
问题 I noticed a bit weird ( and dangerous IMHO ) behavoir in Creating an Array with a Default Value . As stated in Swift 2.1: Collection Types Swift’s Array type also provides an initializer for creating an array of a certain size with all of its values set to the same default value. You pass this initializer the number of items to be added to the new array (called count) and a default value of the appropriate type (called repeatedValue): The point is: same default value ; in order to understand

Creating instance of type without default constructor in C# using reflection

谁都会走 提交于 2019-11-26 04:34:04
问题 Take the following class as an example: class Sometype { int someValue; public Sometype(int someValue) { this.someValue = someValue; } } I then want to create an instance of this type using reflection: Type t = typeof(Sometype); object o = Activator.CreateInstance(t); Normally this will work, however because SomeType has not defined a parameterless constructor, the call to Activator.CreateInstance will throw an exception of type MissingMethodException with the message \" No parameterless

How is an instance initializer different from a constructor?

假如想象 提交于 2019-11-26 04:03:46
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? javamonkey79 This seems to explain it well: Instance initializers are a useful alternative to instance variable initializers whenever: initializer code must catch exceptions, or perform fancy calculations that can't be expressed with an instance variable initializer. You could, of course, always write such code in constructors. But in a class that had multiple constructors, you would have to repeat the code in each constructor. With an

Why is Class.newInstance() “evil”?

跟風遠走 提交于 2019-11-26 03:37:28
问题 Ryan Delucchi asked here in comment #3 to Tom Hawtin\'s answer: why is Class.newInstance() \"evil\"? this in response to the code sample: // Avoid Class.newInstance, for it is evil. Constructor<? extends Runnable> ctor = runClass.getConstructor(); Runnable doRun = ctor.newInstance(); so, why is it Evil? 回答1: The Java API documentation explains why (http://java.sun.com/javase/6/docs/api/java/lang/Class.html#newInstance()): Note that this method propagates any exception thrown by the nullary

Is there a way to instantiate objects from a string holding their class name?

随声附和 提交于 2019-11-26 03:14:11
问题 I have a file: Base.h class Base; class DerivedA : public Base; class DerivedB : public Base; /*etc...*/ and another file: BaseFactory.h #include \"Base.h\" class BaseFactory { public: BaseFactory(const string &sClassName){msClassName = sClassName;}; Base * Create() { if(msClassName == \"DerivedA\") { return new DerivedA(); } else if(msClassName == \"DerivedB\") { return new DerivedB(); } else if(/*etc...*/) { /*etc...*/ } }; private: string msClassName; }; /*etc.*/ Is there a way to somehow

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

假装没事ソ 提交于 2019-11-26 01:59:57
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed last year . 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 these two statements were

`new function()` with lower case “f” in JavaScript

放肆的年华 提交于 2019-11-26 01:49:06
问题 My colleague has been using \"new function()\" with a lower case \"f\" to define new objects in JavaScript. It seems to work well in all major browsers and it also seems to be fairly effective at hiding private variables. Here\'s an example: var someObj = new function () { var inner = \'some value\'; this.foo = \'blah\'; this.get_inner = function () { return inner; }; this.set_inner = function (s) { inner = s; }; }; As soon as \"this\" is used, it becomes a public property of someObj. So

Does python have an equivalent to Java Class.forName()?

北慕城南 提交于 2019-11-26 00:57:19
问题 I have the need to take a string argument and create an object of the class named in that string in Python. In Java, I would use Class.forName().newInstance() . Is there an equivalent in Python? Thanks for the responses. To answer those who want to know what I\'m doing: I want to use a command line argument as the class name, and instantiate it. I\'m actually programming in Jython and instantiating Java classes, hence the Java-ness of the question. getattr() works great. Thanks much. 回答1:

Is there a way to instantiate a class by name in Java?

混江龙づ霸主 提交于 2019-11-26 00:42:00
问题 I was looking as the question : Instantiate a class from its string name which describes how to instantiate a class when having its name. Is there a way to do it in Java? I will have the package name and class name and I need to be able to create an object having that particular name. 回答1: Two ways: Method 1 - only for classes having a no-arg constructor If your class has a no-arg constructor, you can get a Class object using Class.forName() and use the newInstance() method to create an