instantiation

No definition available for static const member with initializer?

痞子三分冷 提交于 2019-12-10 18:02:36
问题 Given: template<class T> struct S { static int const N = 1; }; extern template class S<int>; template<class T> int f( T n ) { return n + S<T>::N; // line 10 } int main() { return f(1); // line 14 } //template class S<int>; // intentionally commented out to trigger error I get: foo.cpp: In function ‘int f(T) [with T = int]’: foo.cpp:10: instantiated from ‘const int S<int>::N’ foo.cpp:10: instantiated from ‘int f(T) [with T = int]’ foo.cpp:14: instantiated from here foo.cpp:10: error: explicit

How to create generic array? [duplicate]

被刻印的时光 ゝ 提交于 2019-12-10 17:32:19
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Java how to: Generic Array creation How to create an array of type T[] in Java? I can't use Arrays.newInstance() since I have no objects of Class<T> . Is there a generic version of newInstance somewhere? My method prototype follows: public <T> T[][] distribute(T ... balls) { T[][] answer = ???? // filling answer with data return answer; } UPDATE Sorry in example above I can take class from balls . But suppose I

Scala case class arguments instantiation from array

ⅰ亾dé卋堺 提交于 2019-12-10 13:46:15
问题 Consider a case class with a possibly large number of members; to illustrate the case assume two arguments, as in case class C(s1: String, s2: String) and therefore assume an array with size of at least that many arguments, val a = Array("a1", "a2") Then scala> C(a(0), a(1)) res9: C = c(a1,a2) However, is there an approach to case class instantiation where there is no need to refer to each element in the array for any (possibly large) number of predefined class members ? 回答1: No, you can't.

Creating instances of public inner classes of generic classes

穿精又带淫゛_ 提交于 2019-12-10 13:30:49
问题 So I have something like the following: public class Enclosing<T extends Comparable<T>> { // non-relevant code snipped public class Inner { private T value; public Inner(T t) { value = t; } } } Everything compiles and the world is happy. However, whenever I try to create an instance of Enclosing.Inner as follows, I can't: new Enclosing<Integer>.Inner(5); The following error happens: Cannot allocate the member type Enclosing<Integer>.Inner using a parameterized compound name; use its simple

Force template static member instantiation

寵の児 提交于 2019-12-10 13:21:52
问题 I'm trying to create the program that executes some code only if the template is instantiated (it will be used for low-level driver initialization). Now I have the following solution. class Initializer { public: Initializer(){ // This code is executed once } void silly() const{ } }; template <class T> class Proxy{ protected: static const Initializer init; }; template<class T> const Initializer Proxy<T>::init; template<class T> class MyTemplate : public Proxy<void>{ public: static void

Class method to create new instance

不想你离开。 提交于 2019-12-10 11:53:53
问题 I am getting into some serious OOP with Python and am running into some issues. I have a class with an initialiser, three private variables and set and get methods for each variable. Additionally, it has an input method that lets the user enter a value for the variables when they are called, rather than requiring an input call in the main function that passes a value to the class set method. This way I can immediately validate the user entry within the method specific to the data requirements

Compiler instantiates the function in a template class even without invoking it

ⅰ亾dé卋堺 提交于 2019-12-10 11:42:36
问题 I had a wrong perception that template function in a class is instantiated only if it's invoked. See the below simple code: template<typename T> struct A { T *p; T& operator * () { return *p; } }; int main () { A<int> ai; // ok int i = *ai; // works fine A<void> av; // compiler complains even "*av" is not called } Just while declaring A<void> , compiler errors out as: error: forming reference to void I tried to specialize the function for void outside the template as below: template<> void A

How to instantiate interface in fragment?

血红的双手。 提交于 2019-12-10 11:22:54
问题 In my Android application, I have a fragment activity which it has a progressbar. This progress bar set to visible when any fragment invoke the interface. Therefore, the code for fragment class is like this: public class MainScreen extends FragmentActivity { public interface OnConnectingToInternet { public void showProgressbar(boolean flag); } // rest of codes . . . // Implementing Interface public void showProgressbar(boolean flag) { if(flag){ myProgressbar.showProgressBar(); } else {

Is it better to assign variables in a class itself or in the class' constructor? [closed]

会有一股神秘感。 提交于 2019-12-10 09:24:44
问题 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 . This is a sort of design question, and I'm sure there are people who do it both ways. But in your opinion, is it better to assign a variable in the class or in the constructor? For example (regardless of syntax or language, this is just to explain): public class Example {

In Java what happens when an object fails to be instantiated?

眉间皱痕 提交于 2019-12-10 03:24:22
问题 I come from a c++ background and I find myself constantly doing this in java: SomeClass sc=new SomeClass(); if(null!=sc) { sc.doSomething(); } What I want to know is what will be in the variable sc if the constructor fails for some reason (like maybe not enough memory). I can' t find a straight answer, and I am worried that I am just wasting my time because maybe if the new operator fails would the program just crash anyway? 回答1: The Java Specification Language 3rd Edition covers your