instantiation

How to instantiate a class in Objective-C that don't inherit from NSObject

蹲街弑〆低调 提交于 2019-11-29 01:24:30
Given this: Person.h: @interface Person { } - (void) sayHello; @end Person.m: #import "Person.h" @implementation Person - (void)sayHello { printf("%s", "Steve"); } @end How do you instantiate the Person? I tried this: Person *p = [Person new]; That doesn't work, nor this: Person *p = [Person alloc]; [UPDATE] I forgot to tell, I already tried inheriting from NSObject, the new and alloc works. I'm just curious if we can instantiate a class that doesn't inherit from NSObject? You absolutely can do so. Your class simply needs to implement +alloc itself, the way that NSObject does. At base, this

Get Enum Instance from Class<? extends Enum> using String value?

女生的网名这么多〃 提交于 2019-11-29 01:08:02
I'm finding it difficult to put the exact question into words, so I'll just give an example. I have two Enum types: enum Shape { CAT, DOG; } enum Color { BLUE, RED; } I have a method: public Object getInstance(String value, Class<?> type); I would like to use the method like: // someValue is probably "RED", and someEnumClass is probably Color.class Color c = getInstance(someValue, someEnumClass); I've been having trouble determining exactly how to implement getInstance() . Once you know the exact Enum class that you want to instantiate, it's easy: Color.valueOf("RED"); But how can this above

Profiling template metaprogram compilation time

前提是你 提交于 2019-11-28 23:09:32
I'm working on a C++ project with extensive compile-time computations. Long compilation time is slowing us down. How might I find out the slowest parts of our template meta-programs so I can optimize them? (When we have slow runtime computations, I have many profilers to choose from, e.g. valgrind's callgrind tool. So I tried building a debug GCC and profiling it compiling our code, but I didn't learn much from that.) I use GCC and Clang, but any suggestions are welcome. I found profile_templates on Boost's site, but it seems to be thinly documented and require the jam/bjam build system. If

Can a static nested class be instantiated in Java?

百般思念 提交于 2019-11-28 19:54:12
问题 From Oracle's Java tutorials I've found this text: As with class methods and variables, a static nested class is associated with its outer class. And like static class methods, a static nested class cannot refer directly to instance variables or methods defined in its enclosing class — it can use them only through an object reference. Note: A static nested class interacts with the instance members of its outer class (and other classes) just like any other top-level class. In effect, a static

Create an instance of derived class from the base class

我的未来我决定 提交于 2019-11-28 12:45:25
I have my abstract base class A : public abstract class A : ICloneable { public int Min { get; protected set; } public int Max { get; protected set; } public A(int low, int high) { this.Min = low; this.Max = high; } //... public object Clone() { return new this(this.Min, this.Max); //<-- ?? } } Which is extended by my class B : public class B : A { public B(int low, int high) : base(low, high) { } //... } Since A is abstract, it cannot be instantiated, but the derived class can. Is it possible to, from class A , create a new instance of class B ? Suppose class A has many derived classes, how

Is it possible to “dynamically” create local variables in Python? [duplicate]

跟風遠走 提交于 2019-11-28 12:11:26
This question already has an answer here: Dynamically set local variable [duplicate] 7 answers Is it possible to create a local variables with Python code, given only the variable's name (a string), so that subsequent calls to "'xxx' in locals()" will return True? Here's a visual : >>> 'iWantAVariableWithThisName' in locals() False >>> junkVar = 'iWantAVariableWithThisName' >>> (...some magical code...) >>> 'iWantAVariableWithThisName' in locals() True For what purpose I require this trickery is another topic entirely... Thanks for the help. If you really want to do this, you could use exec :

no enclosing instance of type… in scope

牧云@^-^@ 提交于 2019-11-28 10:17:37
I investigate java inner classes. I wrote example: public class Outer { public Outer(int a){} public class Inner { public Inner(String str, Boolean b){} } public static class Nested extends Inner{ public static void m(){ System.out.println("hello"); } public Nested(String str, Boolean b , Number nm) { super("2",true); } } public class InnerTest extends Nested{ public InnerTest(){ super("str",true,12); } } } I invoke it from main using following string: new Outer(1).new Inner("",true); I see compile error: java: no enclosing instance of type testInheritancefromInner.Outer is in scope Can you

how to instantiate an object of class from string in Objective-C?

非 Y 不嫁゛ 提交于 2019-11-28 08:59:04
I've a String who's value is the name of the Class[MyClass] which has to be instantiated, and MyClass has a method called -(void)FunctionInClass; i'm using the method called NSClassFromString to instantiate MyClass.I want to know 1) what does NSClassFromString return?? 2) what is id? 3) How to call method -(void)FunctioninClass which is in MyClass using the instance. how should i proceed , i'm doing it in Objective-C for iPhone app? 1) what does NSClassFromString return?? It returns a Class, which means you can do [[NSClassFromString(@"MyClass") alloc] init]; see Mac Dev Center Docs for more

How to instantiate an object with a private constructor in C#?

余生长醉 提交于 2019-11-28 08:55:26
I definitely remember seeing somewhere an example of doing so using reflection or something. It was something that had to do with SqlParameterCollection which is not creatable by a user (if I'm not mistaken). Unfortunately cannot find it any longer. Can anyone please share this trick here? Not that I consider it a valid approach in development, I'm just very interested in the possibility of doing this. // the types of the constructor parameters, in order // use an empty Type[] array if the constructor takes no parameters Type[] paramTypes = new Type[] { typeof(string), typeof(int) }; // the

Automatically count the number of instantiated classes in a TMP?

不问归期 提交于 2019-11-28 08:33:33
Given a template metaprogram (TMP), do C++ compilers produce build statistics that count the number of instantiated classes? Or is there any other way to automatically get this number? So for e.g. the obiquitous factorial #include <iostream> template<int N> struct fact { enum { value = N * fact<N-1>::value }; }; template<> struct fact<1> { enum { value = 1 }; }; int main() { const int x = fact<3>::value; std::cout << x << "\n"; return 0; } I would like to get back the number 3 (since fact<3>, fact<2>, and fact<1> are instantiated). This example if of course trivial, but whenever you start