constructor

Checking that floating points arguments are correct

隐身守侯 提交于 2019-12-23 17:16:37
问题 I want to write a class representing Markov chain (let's name it MC ). It has a constructor, which takes the state transition matrix (that is, vector<vector<double>> . I suppose, it is a good idea to check it is really a matrix (has the same number of rows and columns) and is really a transition matrix: all the numbers in it are probabilities, that is, no less than 0.0 and no greater than 1.0 , and for every row the sum of its elements is 1.0 . However, there is a problem which arises from

Why does a struct, that has another struct wrapped in a union as a member not compile without an explicit default constructor?

房东的猫 提交于 2019-12-23 17:01:39
问题 This is the relationship I am talking about: struct A{ int i = 1; }; struct B{ union{A a;}; }; void main(){ B b; }; In this constellation, my compiler (vs2015) complains about the default constructor of B B::B(void) beeing deleted, with the note that the compiler has generated B::B : ../test.cpp(155): error C2280: "B::B(void)" : Es wurde versucht, auf eine gelöschte Funktion zu verweisen ../test.cpp(152): note: Compiler hat hier "B::B" generiert (sorry, I could not convince msvc to talk

Extend a Primitive only in current scope

独自空忆成欢 提交于 2019-12-23 16:52:34
问题 Is it possible to copy a primitive like String so it can be extended only in the current function? var foo = function() { var String = // somehow clone String (the primitive function) String.prototype.first = function() { return this.charAt(0) }; return 'abc'.first(); } foo(); // returns 'a' 'abc'.first(); // returns TypeError "abc".first is not a function The solution must either copy the primitive which doesn't seem possible, or destroy the new method after the function is complete... 回答1:

Scala.js does not see a JS *class* in the global scope, but sees a constructor function

妖精的绣舞 提交于 2019-12-23 16:13:58
问题 While trying to write scalajs facade for javascript class, i am getting the following error: "error Uncaught TypeError: $g.MyRect2 is not a constructor" in chrome console. My javascript class defination is as follows: class MyRect2 { constructor(height, width) { this.height = height; this.width = width; } area() { return this.height * this.width } } Then I imported it as follows in scala @js.native class MyRect2(h:Int,w:Int) extends js.Object { val height:Int = js.native val width:Int = js

Constructor of class with conflicting name

让人想犯罪 __ 提交于 2019-12-23 16:02:05
问题 I am using clang to compile my code using the c++14 dialect. Take the following example: class x { int _i; public: x(int i) { this->_i = i; } }; void x() { } void f(class x my_x) { // Do something here } int main() { /* f(x(33)); // Doesn't work f(class x(33)); // Doesn't work */ // This works: class x my_x(33); f(my_x); typedef class x __x; f(__x(33)); } Here I have a class named x whose name conflicts with a function with the same name. To distinguish between x the class and x the function,

Why is forwarding reference constructor called instead of copy constructor?

余生颓废 提交于 2019-12-23 15:47:57
问题 Given the following code #include <iostream> using namespace std; template <typename Type> struct Something { Something() { cout << "Something()" << endl; } template <typename SomethingType> Something(SomethingType&&) { cout << "Something(SomethingType&&)" << endl; } }; int main() { Something<int> something_else{Something<int>{}}; auto something = Something<int>{}; Something<int>{something}; return 0; } I get the following output Something() Something() Something(SomethingType&&) Why is the

Super constructor in java

牧云@^-^@ 提交于 2019-12-23 15:45:02
问题 Please explain public class Contact { private String contactId; private String firstName; private String lastName; private String email; private String phoneNumber; public Contact(String contactId,String firstName, String lastName, String email, String phoneNumber) { super(); //what does standalone super() define? With no args here? this.firstName = firstName; this.lastName = lastName; //when is this used?, when more than one args to be entered? this.email = email; this.phoneNumber =

SerialForms.pas(17): W1010 Method 'Create' hides virtual method of base type 'TComponent'

被刻印的时光 ゝ 提交于 2019-12-23 14:21:08
问题 I created a class FormInfo = class (TComponent) private FLeftValue : Integer; FTopValue : Integer; FHeightValue : Integer; FWidthValue : Integer; public constructor Create( AOwner : TComponent; leftvalue : integer; topvalue : integer; heightvalue : integer; widthvalue : integer); protected procedure GetChildren(Proc: TGetChildProc; Root: TComponent); override; function GetChildOwner: TComponent; override; //procedure SetParentComponent(Value : TComponent); override; published property

Double free of child object after using the copy constructor

久未见 提交于 2019-12-23 14:19:54
问题 I am having trouble figuring out why (it seems like) an object is being destructed twice. If i create a object of a class (B) which contains an object of another class (A) and i copy this object. the copied object is destructed twice. Altough it looks like this. I am unable to figure out this output. I have created the following (minimum?) example which seems to trigger my issue: #include <stdio.h> #include <stdint.h> template <class T> class A { public: A() { myCtr = ++ctr; printf("class A

Double free of child object after using the copy constructor

限于喜欢 提交于 2019-12-23 14:19:09
问题 I am having trouble figuring out why (it seems like) an object is being destructed twice. If i create a object of a class (B) which contains an object of another class (A) and i copy this object. the copied object is destructed twice. Altough it looks like this. I am unable to figure out this output. I have created the following (minimum?) example which seems to trigger my issue: #include <stdio.h> #include <stdint.h> template <class T> class A { public: A() { myCtr = ++ctr; printf("class A