constructor

Define constructor prototype with object literal

浪尽此生 提交于 2020-01-16 05:07:06
问题 Which method below is best to define a constructor prototype and why? Method 1: MyConstructor.prototype.myFunction1 = function(){}; MyConstructor.prototype.myFunction2 = function(){}; Method 2: MyConstructor.prototype = { myFunction1: function(){}, myFunction2: function(){} }; I'm mostly concerned about speed. Thanks! 回答1: I would say there wouldn't be much of a difference. Using an object literal to assign to the Object.prototype is something you can't do if you're assigning the prototype

Assigning parameter value in function declaration?

谁说胖子不能爱 提交于 2020-01-16 03:19:41
问题 I don't understand why in this constructor declaration, the input parameter is assigned 2. What does it mean? Does it mean that by default (unless something else is passed), size will be 2? Graph(int size = 2); I've never seen syntax like this, so I don't even know how to Google it :/ Thanks in advance! 回答1: You're right, the parameter value will be 2 by default. So you can call it normally: Graph g(5); in which case size will be equal to 5, or you can call it without providing a value: Graph

Object cannot be cast from DBNull to other types in constructor

回眸只為那壹抹淺笑 提交于 2020-01-15 19:14:06
问题 I need to add into the list one object CompanyDetails, so I get data from my database, and load it into constructor. result.Add(new CompanyDetails() { Name = dr["name"].ToString(), City = dr["city"].ToString(), StreetName = dr["streetName"].ToString(), StreetNr = Convert.ToInt32(dr["apartmentNr"]), Tax = int.Parse(dr["TAX"].ToString() )}); StreetNr and Tax can have null value. And when I'm trying to run it i get the error: Object cannot be cast from DBNull to other types How can I fix it? I

Temporary non-const istream reference in constructor (C++)

别等时光非礼了梦想. 提交于 2020-01-15 11:03:01
问题 It seems that a constructor that takes a non-const reference to an istream cannot be constructed with a temporary value in C++. #include <iostream> #include <sstream> using namespace std; class Bar { public: explicit Bar(std::istream& is) {} }; int main() { istringstream stream1("bar1"); Bar bar1(stream1); // OK on all platforms // compile error on linux, Mac gcc; OK on Windows MSVC Bar bar2(istringstream("bar2")); return 0; } This compiles fine with MSVC, but not with gcc. Using gcc I get a

Temporary non-const istream reference in constructor (C++)

耗尽温柔 提交于 2020-01-15 11:02:09
问题 It seems that a constructor that takes a non-const reference to an istream cannot be constructed with a temporary value in C++. #include <iostream> #include <sstream> using namespace std; class Bar { public: explicit Bar(std::istream& is) {} }; int main() { istringstream stream1("bar1"); Bar bar1(stream1); // OK on all platforms // compile error on linux, Mac gcc; OK on Windows MSVC Bar bar2(istringstream("bar2")); return 0; } This compiles fine with MSVC, but not with gcc. Using gcc I get a

How to differentiate fill constructor and range constructor in C++11?

喜你入骨 提交于 2020-01-15 09:48:05
问题 I suspect the prototypes of fill constructor and range constructor of std::vector (and many other STL types) given in this webpage are not right, so I implement a NaiveVector to mimic these two prototypes. My code is: #include <iostream> #include <vector> using namespace std; template <typename T> struct NaiveVector { vector<T> v; NaiveVector(size_t num, const T &val) : v(num, val) { // fill cout << "(int num, const T &val)" << endl; } template <typename InputIterator> NaiveVector

Getting the full path of the function calling the constructor

ε祈祈猫儿з 提交于 2020-01-15 09:27:29
问题 I'd like the constructor of a class to automatically determine the full path to the calling function, so that that class can write a file that's guaranteed to be in the caller's directory (instead of whatever happens to be the pwd() ). So, I have the following setup: In some_path/test.m : function test SomeClass() end In some_path/some_subdir/SomeClass.m : classdef SomeClass < handle methods function obj = SomeClass() evalin('caller', 'mfilename(''fullpath'')') end end end When I call test()

Inherit and overload default constructor

為{幸葍}努か 提交于 2020-01-15 09:10:08
问题 I've been searching for this and I'm amazed I haven't found anything. Why can't I inherit a base class constructor using using declaration and add an overload in the derived class? I'm using Visual C++ 2013, the base class constructor is ignored when default-constructing b : error C2512: 'B' : no appropriate default constructor available I've dealt with this by re-defining the constructors, but I don't like that. This is just a minimal example, it wouldn't bother me if I had only one base

Strange constructor

*爱你&永不变心* 提交于 2020-01-15 05:06:40
问题 Well, I'm gonna be pretty straightforward here, I just have a piece of code in c++ which I'm not sure I really understand and need some help with. Ok, to simplify lets just say I have a class that is defined like this: (the real class is a little bit more complicated, but this is what matters) class myClass : public Runnable { Semaphore *m_pMySemaphore; __Queue<Requests> *m_pQueue; Request m_Request; VetorSlotBuffer *m_vetorSlotBuffer; } Up to here nothing is wrong, myClass is just a regular

Overriding default constructor in Java

余生颓废 提交于 2020-01-14 15:32:07
问题 Pretty easy question, but anyway: is there any reason to override default constructor like this: public SomeObject(){ } It is public. It is does not have any logic. So, is there necessary? I didn't see the whole picture? Appreciate all your help. 回答1: One reason to define an empty no-arg constructor is if there is also a non-default constructor and the no-arg constructor is still desired to be accessible (public or protected). This is because any [other] constructor definition will prevent