constructor

Constructor for array of variable size

送分小仙女□ 提交于 2019-12-13 04:03:53
问题 I want to code a constructor for an array of size x with x being a parameter speciified in main(). My class: public class CharA { private char[] stack; private int n = 0; public void CharA (int max) { this.stack = new char[max]; this.n = max; } My main(): public class CharTest { public static void main (String args) { CharA stack1 = new CharA(100); } } The error: CharTest.java:5: cannot find symbol symbol : constructor CharA(int) location: class CharA CharA stack1 = new CharA(100); ^ There

Trouble understanding super() when calling multiple parents

五迷三道 提交于 2019-12-13 03:50:02
问题 I've been doing research on Python 3 (my code sample uses 3.7.2) and how to properly use super() when a class inherits more than one class. I've read this page, and this page and this article. I think the problem is that the SO links are for an older version of Python, while the article is for Python 3, but it's still confusing. Suppose I had the following code (don't worry if you think that the relationship can be modeled better, this is just an example to illustrate my problem): class

Writing constructor of template class with template argument-depending parameters

£可爱£侵袭症+ 提交于 2019-12-13 03:44:14
问题 Consider the following two classes with constructors that accept wildly different sets of parameters. class A { public: A(int x, double y) { //do something } }; class B { public: B(const std::vector<bool>& x) { //do something } }; Now, I want to write a class template TpC that will have an object (which might be A or B or something else). In other words, I want to be able to use the following: int x; double y; std::vector<bool> z; TpC<A> obj_a (x, y); TpC<B> obj_b (z); How can I write a

RegExp constructor properties input

情到浓时终转凉″ 提交于 2019-12-13 03:03:50
问题 Setting the global flag to true: <script> var str="I am really puzzled up"; var str1="Being puzzled is first step towards understanding"; var patt=new RegExp("puzzled","gi"); patt.exec(str); alert(RegExp.$_); //I am really puzzled up *[1] patt.exec(str1); alert(RegExp.$_); //I am really puzzled up *[2] patt.exec(str1); alert(RegExp.$_); //Being puzzled is first step towards understanding *[3] </script> Without setting the global flag to true: <script> var str="I am really puzzled up"; var

C++ move assignment to uninitialized object?

偶尔善良 提交于 2019-12-13 02:59:33
问题 As follow up to: Double free of child object after using the copy constructor I followed the rule of 5 as suggested. But now it seems like the move assignment is happening on an uninitialized object (to object id 0)? I expected it to move from object 3 to object 2. I have created the following (minimum?) example which seems to trigger my issue: #include <stdio.h> #include <stdint.h> class A { public: A() { myCtr = ++ctr; printf("class A default Constructor - object id: %u\n", myCtr); } A

What is the proper way to call default constructors?

血红的双手。 提交于 2019-12-13 02:57:17
问题 Lately I have been having difficulties with constructors, and the different attempts from other questions and guides always have some form of segfault waiting for me at runtime (making compiling 80% of my time spent programming). The following example shows the basic idea on what I am trying to accomplish: struct Coord3{ float x, y, z; Coord3() {x=0;y=0;z=0;};///Is this Correct? }; struct Stat{ int Str,Dex,Int; Stat(){Str=0;Dex=0;Int=0;}; }; struct Item{ Stat myStats; Item(){...}; }; class

Passing compare function for a generic class

点点圈 提交于 2019-12-13 02:49:45
问题 I want to create a priority queue for which I am using a heap(using array).The priority queue will be generic thus accept all data types as long as the client pass a compare function through constructor to compare the two types. How can I create a constructor that will accept the compare function as a parameter? Moreover how can I make the compare function to be called when I check return (Type a==Type b) Eg. struct node{ string val1; string val2; vector<node *> connectedNodes; }; int

JavaScript constructors

痞子三分冷 提交于 2019-12-13 02:44:19
问题 I do not understand quite completely how to apply constructors on this object creation method: var MyObject = { ... }; I know that you can do: var MyObject = new Object(); MyObject.prototype.constructor = function(props) { ... } or... function MyObject(prop1, prop2) { this.prop1 = prop1; ... } Can I do something like this? var MyObject = { MyObject: function(prop1, prop2) { ... } } 回答1: No, you can't, that would simply create a (static) method on MyObject -- MyObject.MyObject . In JavaScript,

C++ copy constructor behaviour [duplicate]

蹲街弑〆低调 提交于 2019-12-13 02:17:10
问题 This question already has answers here : What are copy elision and return value optimization? (4 answers) Closed 5 years ago . There is a part of C++ code I don't really understand. Also I don't know where should I go to search information about it, so I decided to ask a question. #include <iostream> #include <string> using namespace std; class Test { public: Test(); Test(Test const & src); Test& operator=(const Test& rhs); Test test(); int x; }; Test::Test() { cout << "Constructor has been

Constructor inheritance for class derived from template class in visual studio 2015 rc

微笑、不失礼 提交于 2019-12-13 02:13:59
问题 According to the page of msvs2015rc new features constructor inheritance should be supported. Yes, it works in simple cases like this: struct B { B(int) {} }; struct D : B { using B::B; // now we can create D object with B's constructor }; But if I'll try to create more complex example: template <class T> struct B { B(int) {} }; template <template <class> class C, class T> struct D : C<T> { using C<T>::C; }; int main() { D<B,int> d(42); return 0; } I've got compiler errors: error C2039: 'C':