instantiation

Order of initialization/instantiation of class variables of derived class and invoking of base class constructor

醉酒当歌 提交于 2020-01-16 05:34:05
问题 I want to figure out the order of 1) initialization/instatiation of derived class variables 2) invoking of base class constructor in this code snippet public class base { int y = 1; public base() { y = 2; function(); } void function () { System.out.println("In base Value = " + String.valueOf(y)); } public static class derived extends base { int y = 3; public derived() { function(); } void function () { System.out.println("In derived Value = " + String.valueOf(y)); } } public static void main

why i cant instantiate objects inside a switch-case block

好久不见. 提交于 2020-01-12 15:54:53
问题 my code has 3 classes n_hexa,n_octa,n_bin. The code is here switch(choice) { case 1: cin>>n; n_hexa nx(n); break; case 2: cin>>n; n_octa no(n); break; case 3: cin>>n; n_bin nb(n); break; } on compiling it gives a message " crosses initialisation of n_hexa " for line of n_octa 回答1: If you want to have temporary objects inside a case, you'll need to scope them properly. switch(choice) { case 1: { cin>>n; n_hexa nx(n); break; } case 2: { cin>>n; n_octa no(n); break; } case 3: { cin>>n; n_bin nb

How do I write a custom init for a UIView subclass in Swift?

限于喜欢 提交于 2020-01-11 14:48:53
问题 Say I want to init a UIView subclass with a String and an Int . How would I do this in Swift if I'm just subclassing UIView ? If I just make a custom init() function but the parameters are a String and an Int, it tells me that "super.init() isn't called before returning from initializer". And if I call super.init() I'm told I must use a designated initializer. What should I be using there? The frame version? The coder version? Both? Why? 回答1: The init(frame:) version is the default

Instantiate class from name in MATLAB

陌路散爱 提交于 2020-01-10 14:10:35
问题 I'm trying to list classes I created in some folder in my Matlab folder - using only their name (class name) as an example, I have a class called 'SimpleString' - and I'm aiming to instantiate an object from that class, if all I know is that its name is 'SimpleString' So in realtime, I'd like to find out what classes are in a folder (done), then be able to instantiate any of those classes (my question) Thanks 回答1: You can use eval to instantiate the class using just the class name. instance =

Why can variables be assigned to interface type?

时光总嘲笑我的痴心妄想 提交于 2020-01-07 03:03:13
问题 I'm new to Java and am trying to learn the concept of interface. I saw the code below online. I understand that interface can't be instantiated. My question is, WatchService, Path, WatchKey and WatchEvent are all interface, how come variables can be assigned to interface type? Is is the same as instantiation? import java.io.IOException; import java.nio.file.FileSystems; import java.nio.file.Path; import java.nio.file.Paths; import static java.nio.file.StandardWatchEventKinds.ENTRY_CREATE;

Can I use `-fno-implicit-templates` feature only for one template?

跟風遠走 提交于 2020-01-06 08:33:24
问题 Is it possible to forbid implicit instantiation, like -fno-implicit-templates does, but only for one template? I have a problem with implicit instantiation of incomplete template, which causes compilation failure (part of implementation is hidden in source file, and I don't want to have it in other TUs). -fno-implicit-templates solves the problem, but at cost of problems with using STL and other templates. 回答1: You can try to use explicit template instantiation. Put explicit template

Class Member Access on Instantiation

别等时光非礼了梦想. 提交于 2020-01-04 23:42:11
问题 In PHP 5.4, I believe something like this is valid: echo ( new DateTime( '2014-04-05 10:36am' ))->format( 'Y-m-d g:ia' ); On PHP 5.3, I currently do something like this: $date = new DateTime( '2014-04-05 10:36am' ); echo $date->format( 'Y-m-d g:ia' ); Any way to combine those two lines into a single line in PHP 5.3 (and I don't mean by concatenating the lines)? Or will I have to upgrade to >=5.4 to have that option? 回答1: Will I have to upgrade to >=5.4 to have that option? Yes. You need to

Explicit instantiation of function template specialization

元气小坏坏 提交于 2020-01-04 15:13:15
问题 I am trying to create a global function template specialized for some given types. It looks something like that: A.h (primary template, template specialization, extern) template <typename T> void foo() { std::cout << "default stuff" << std::endl; } template<> void foo<int>() { std::cout << "int stuff" << std::endl; } extern template void foo<int>(); A.cpp (explicit instantiation) template void foo<int>(); B.h void bar(); B.cpp (includes A.h) void bar() { foo<int>(); } main.cpp foo<int>(); bar

java.lang.RuntimeException: Unable to instantiate service

假装没事ソ 提交于 2020-01-04 12:24:33
问题 Error log after installing and running the live wallpaper Basically, I get the error on live wallpaper app. It crashes when I'm setting the wallpaper. What's the problem? com.snailreactors.thestorm.SBLiveWallpaper: java.lang.ClassNotFoundException: com.snailreactors.thestorm.SBLiveWallpaper at android.app.ActivityThread.handleCreateService(ActivityThread.java:2347) at android.app.ActivityThread.access$1600(ActivityThread.java:130) at android.app.ActivityThread$H.handleMessage(ActivityThread

Create subclass from superclass static main

狂风中的少年 提交于 2020-01-03 19:05:14
问题 I have a generic, abstract class ( SuperClass ). I want to have there a main method, that would be a default main for each subclass and would do the same, but with proper subclass object that derived and called it. Like this: public abstract class SuperClass { // some code here... public static void main(String args[]) { // here instantiate the subclass // extending this SuperClass, and call // some methods } } public SubClass extends SuperClass { // here just implement some // abstract