constructor

Subclass vs Wrapper - Constructor With An Additional Parameter

匆匆过客 提交于 2019-12-11 10:43:01
问题 Which is generally considered the more preferred method when trying to add a constructor with an additional parameter? A subclass or a wrapper? That being, creating a subclass of the class and then just using that subclass' constructor? Or adding a wrapper method which will take the extra parameter and return an object with that parameter set? Thank you for your time! EDIT: I don't have access to the superclass's code. 回答1: The answer is language dependent. In C#/.NET, you would typically use

How to initialize a non-POD member in Union

非 Y 不嫁゛ 提交于 2019-12-11 10:29:15
问题 In c++11, Union supports non-POD member. I want to initialize a non-POD member in the constructor. On wikipedia c++11 page, it uses a placement 'new' to initialize a non-POD member. #include <new> // Required for placement 'new'. struct Point { Point() {} Point(int x, int y): x_(x), y_(y) {} int x_, y_; }; union U { int z; double w; Point p; // Illegal in C++03; legal in C++11. U() {new(&p) Point();} // Due to the Point member, a constructor definition is now required. }; I am wondering is

Odd behaviour of python's class [duplicate]

不想你离开。 提交于 2019-12-11 10:26:46
问题 This question already has answers here : “Least Astonishment” and the Mutable Default Argument (31 answers) Closed 5 years ago . Here is a python class: class TxdTest(object): def __init__(self, name = '', atrributes = []): self.name = name self.attributes = atrributes and then I use it like this: def main(): for i in range(3): test = TxdTest() print test.name print test.attributes test.name = 'a' test.attributes.append(1) so, what's the result? The result is: [] [1] [1, 1] Why the 'self

error constructor controller MvC 4

让人想犯罪 __ 提交于 2019-12-11 10:19:33
问题 public partial class RegistrationController : Controller { private readonly IPartnerRepository _partnerRepository; public RegistrationController(IPartnerRepository partnerRepository) { _partnerRepository =partnerRepository; }} I use autoface to the dependancy injection and i have this error: ` No parameterless constructor defined for this object. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information

weird C++ constructor/copy constructor issues in g++

妖精的绣舞 提交于 2019-12-11 10:15:51
问题 #include <iostream> using namespace std; class X { public: X() { cout<<"Cons"<<endl; } X(const X& x){ cout<<"Copy"<<endl; } void operator=(const X& x){ cout<<"Assignment called"; } }; X& fun() { X s; return s; } int main(){ X s = fun(); return 0; } This code calls the copy constructor also. Why does this work? I recall that the first time I ran this program, it seg faulted. But after a while, it started calling this copy cons. and now works!! Wierd. But if I replace, fun() as follows: X fun()

What is the actual use of the default constructor in java?

核能气质少年 提交于 2019-12-11 10:14:10
问题 I have read in many sources and books that constructor is used to initialise the fields when an object is created. I have also read that JVM provides a default constructor if I don't mention one. What is the purpose of a constructor if it is not required to initialise the fields like the one I have mentioned below? I also understand that a constructor without parameter is required as it is necessary during object creation when an argument is not passed (when programmer-defined constructors

In Java, can a method/constructor declaration appear inside another method/constructor declaration?

邮差的信 提交于 2019-12-11 10:09:18
问题 In Java, can a method/constructor declaration appear inside another method/constructor declaration? Example: void A() { int B() { } } I think not, but I'd love to be reassured. 回答1: No . it is not compilable. 回答2: No this is not possible. For reference: http://download.oracle.com/javase/tutorial/java/javaOO/methods.html 回答3: Not directly, but you can have a method in a class in a method: class A { void b() { class C { void d() { } } } } 回答4: This is not possible in java. However this can

Looking for a Post-Constructor Event of a Control

﹥>﹥吖頭↗ 提交于 2019-12-11 09:59:39
问题 I have made a Label subclass and need to initialize a few of its Properties. Some I can set in the constructor, but others are being reset in the designer code of the form, so they must be set after the designer has done its InitializeComponent but before the Paint event runs, which needs them in place. Of course the control should be self-sufficient, so I can't add anything to the form's code. I am using a workaround now: I set a flag bool needsInit = true; which I check in the Paint event.

C#: How to create a generic superclass to be extended by non-generic subclasses

送分小仙女□ 提交于 2019-12-11 09:14:36
问题 I have a bunch of Repository classes which all look a bit like the following. Note that I have omitted certain methods; I just want you to get a flavour. public class SuggestionRepository : ISuggestionRepository { private IUnitOfWork _unitOfWork; public SuggestionRepository(IUnitOfWork unitOfWork) { _unitOfWork = unitOfWork; } public Suggestion Load(int id) { Suggestion suggestion; suggestion = _unitOfWork.Load<Suggestion>(id); return suggestion; } public IQueryable<Suggestion> All { get {

Constructor parameter style

你说的曾经没有我的故事 提交于 2019-12-11 09:09:30
问题 Lets say my file looks like this: #include <iostream> #include <string> using namespace std; class testclass{ public: string name; //testclass(const string& sref){ // name = sref; //} testclass(string str){ name = str; } ~testclass(){} }; int main(){ testclass t1("constStringRef"); cout << t1.name << '\n'; } What are the differences between constructor 1 and 2 given the following constructor-call: testclass tobj("tmemberstring"); Here is what i thought of: I know that passing by reference