constructor

How does multiple inheritance work with the super() and different __init__() arguments?

空扰寡人 提交于 2019-12-18 10:23:15
问题 I'm just diving into some more advanced python subjects (well, advanced to me at least). I am now reading about multiple inheritance and how you can use super(). I more or less understand the way the super function is used, but (1) What's wrong with just doing it like this ?: class First(object): def __init__(self): print "first" class Second(object): def __init__(self): print "second" class Third(First, Second): def __init__(self): First.__init__(self) Second.__init__(self) print "that's it"

Why doesn't Java have initializer lists like in C++?

和自甴很熟 提交于 2019-12-18 10:03:41
问题 In C++, you can use an initializer list to initialize the class's fields before the constructor begins running. For example: Foo::Foo(string s, double d, int n) : name(s), weight(d), age(n) { // Empty; already handled! } I am curious why Java does not have a similar feature. According to Core Java: Volume 1 : C++ uses this special syntax to call field constructors. In Java, there is no need for it because objects have no subobjects, only pointers to other objects. Here are my questions: What

Inaccessible due to its protection level?

三世轮回 提交于 2019-12-18 09:47:02
问题 I'm still quite new to coding in general, and while this simple program is only meant to be a test to learn how constructors work, I'd still like to know why I'm getting this error. using System; public class methodTest { int a; int b; int c; public methodTest(int i, int j, int k) { a = i; b = j; c = k; } } public class methodObj { static void Main() { methodTest obj = new methodTest(10, 20, 30); Console.WriteLine("obj = " + obj.b); Console.ReadKey(); } } I'm not entirely sure why I'm getting

type requirements for std::vector<type>

拜拜、爱过 提交于 2019-12-18 09:06:40
问题 I am still confused about the requirements for a type to be used with a std::vector in C++11, but this may be caused by a buggy compiler (gcc 4.7.0). This code: struct A { A() : X(0) { std::cerr<<" A::A(); this="<<this<<'\n'; } int X; }; int main() { std::vector<A> a; a.resize(4); } works fine and produces the expected output, indicating that the default ctor (explicitly given) is called (and not an implicit copy ctor). However, if I add a deleted copy ctor to the class, viz struct A { A() :

constructors basic (reference to distance is ambiguous)

删除回忆录丶 提交于 2019-12-18 09:04:32
问题 I was practicing constructors. Below is the code which I was practicing but got an error that "reference to distance is ambiguous" I could not identify my error, Please help me. I have been trying that . #include <iostream> #include <conio.h> using namespace std; //distance class distance { public: distance(int met, int cen); distance(int met); void display(); private: int meters; int centimeters; }; distance::distance(int met, int cen){ cout<<"Object have been initialized and assigned the

Bodyless constructor in non-abstract C# class

自闭症网瘾萝莉.ら 提交于 2019-12-18 09:02:24
问题 I'm trying to understand the following class provided in the MVC framework; it seems like the class should be abstract, but it is not, and yet this class compiles. Is the class actually abstract despite the missing "abstract" keyword? What am I missing here? namespace Microsoft.AspNet.Identity.EntityFramework { public class IdentityUser : IUser { public IdentityUser(); public IdentityUser(string userName); public virtual ICollection<IdentityUserClaim> Claims { get; } public virtual string Id

What is the meaning of nil owner in component constructor

为君一笑 提交于 2019-12-18 08:57:00
问题 I was looking at this question and I'm wondering now, what is the meaning of nil as the owner in component constructor. SomeComponent := TSomeComponent.Create(nil); I know, that I should free it by myself when using this constructor, but is that the only reason to pass the owner at creation ? And what happens, when I forget to free it and close my application - does it mean that this object remains in memory as a garbage ? Thanks a lot :) 回答1: It means that you're responsible for freeing it

Java Class Constructor Parameters with range limits

烂漫一生 提交于 2019-12-18 08:55:26
问题 I'm new to Java and I'm asking this question just to help me better understand OOP. Let's say I'm defining a new Class called Hour. To instantiate this Class, we need to specify an integer to indicate the hour of this instance. Hour hr = new Hour(16); // this means to define an hour that indicates 4pm. So when we define the Hour Class here, the parameter for the constructor should within the range [0, 24). How can we define such a parameter and can I throw an error when a parameter that is

Why is my constructor still called even if the class and constructor case are different?

六眼飞鱼酱① 提交于 2019-12-18 08:47:53
问题 I am surprised for why the constructor is called when we have different class and constructor name. Constructor name is starting with small "r"? class Registration{ function registration(){ echo "Constructor is called."; } } $obj = new Registration(); //$obj->registration(); Outputs: Constructor is called. Modification: Does this case-insensitive behavior depends on php versions we are using? 回答1: php is case-insensitive (sometimes). The following would work as well: CLASS REGISTRATION {

Why would the implicitly generated constructor (et al.) be more efficient than a user-defined (trivial) one?

瘦欲@ 提交于 2019-12-18 07:37:34
问题 I read this article from D. Kalev this morning about the new c++11 feature "defaulted and deleted functions", and can't understand the part about performance, namely: the manual definition of a special member function (even if it's trivial) is usually less efficient than an implicitly-defined one. By googling to find an answer, I found another article of the same author: the synthesized constructor and copy constructor enable the implementation to create code that's more efficient than user