constructor

How to call class constructor having its name in text variable? [Python]

♀尐吖头ヾ 提交于 2019-12-12 04:04:48
问题 Let's assume we have some classes defined and available in global namespace. In example: class Point: def __init__(self, x, y): self.x = x self.y = y class Vector: def __init__(self, alpha, r): self.x = r * cos(alpha) self.y = r * sin(alpha) # and many others... How to do this: class_name = 'Point' x = 14.361 y = -8.100 code_str = 'class_object = ' + class_name + '(' + str(x) + ', ' + str(y) + ')' exec code_str # That evaluates to: "class_object = Point(14.361, -8.100)" print class_object.x,

Constructor, Copy Constructor and Stack Creation : C++

痴心易碎 提交于 2019-12-12 03:54:23
问题 This question is regarding Function Stack Creation. Suppose we create a function fn(int a,char b) and call from main fn(A,B) , in this case when the function is called a fn. stack is created with return address, Stack pointer (etc) where local variables and parameters are created and on return is destroyed. I have a few questions: 1) For our parameterized constructor suppose myClass{ int a; char c; public: myClass(int a,char c) { this->a=a; this->c=c; } }; does the constructor myClass(int a

Structuring SuperClasses

99封情书 提交于 2019-12-12 03:53:35
问题 Say i have 3 classes like this: |Abstract Class: Building| |int windows; | |int rooms; | | | |Abstract Class: House extends Building| |int familyMembers; | | |Class: MobileHome extends House| |int wheels; | Now i am trying to figure out the best way to structure my program, because obviously the number of windows and rooms will depend on the subclass of House , however all buildings have windows (at least for the sake of this program they do). So that is why they are in the building class but

Compile time error “final variable is not initialized”

余生长醉 提交于 2019-12-12 03:48:47
问题 I have an issue, while trying few code snippets i came across a code class O { final int i; O() { i=10; } O(int j)// error here as THE BLANK FINAL FIELD i IS NOT INITIALIZED { j=20; System.out.println(j); } } class Manager3 { public static void main(final String[] args) { O n1=new O(); //O n2=new O(10); //n1.i=20; //System.out.println(j1.i); } } but if i comment the constructor with parameter i do not get any errors. My question is why am i getting this compile time error when i put both the

Why does constructor with arg undefine the default constructor?

痞子三分冷 提交于 2019-12-12 03:46:49
问题 Consider - public class Class_A { public void func() {...} public void func(int a){...} All three - Class_A a = new Class_A(); // legal a.func(); // legal a.func(1); // legal But After constructor with arg like public Class_A (int a){...} is added to Class_A , the default constructor become to be - Class_A a = new Class_A(); // The constructor Class_A() is undefined Thats force me to add public Class_A() {/*Do Nothing*/} into Class_A . Since each class has default constructor , why doesn't

If class A modifies its construction parameters, can I initialize const A's with const parameters?

时光毁灭记忆、已成空白 提交于 2019-12-12 03:44:20
问题 Suppose I have class A final { int& ir; public: A(int& x) : ir(x) { } void set(int y) { ir = y; } // non-const method! int get() const { return ir; } }; and const int i; Obviously I can't have A a(i); since that would breaks constness. But I also cannot have const A a(i); despite the fact that this will not break constness de-facto. C++ doesn't support "const-only" ctors, e.g. in this case one which would take a const int& . Is there a way to get const A a wrapping a reference to i - other

Why do I get Infinite loop(Stackoverflow error) while creating an object in a class with constructor? [duplicate]

旧街凉风 提交于 2019-12-12 03:39:19
问题 This question already has answers here : stackoverflow error in class constructor (2 answers) Why am I getting a StackOverflowError exception in my constructor (3 answers) Closed 2 years ago . This is my code : public class ConstructorsDemo{ public static void main(String a[]){ Cons1 c1 = new Cons1(); } } class Cons1{ Cons1 c = new Cons1();// the error is in this line Cons1(){ //does somwthing } } So I get an infinite loop error here (Stackoverflow). However it's fine if I comment out any of

Is this ctor / use of ctor correct?

谁都会走 提交于 2019-12-12 03:33:07
问题 SingleMonitorInfo::SingleMonitorInfo(MONITORINFOEX* lpMONITORINFOEX) :rcMonitorArea(lpMONITORINFOEX->rcMonitor), rcWorkArea(lpMONITORINFOEX->rcWork), dwStatusFlags(lpMONITORINFOEX->dwFlags), szDeviceName({ '\0' }), szMonitorName({ '\0' }), szMonitorDescription({ '\0' }), lpPixelArray(NULL) { wcscpy_s(SingleMonitorInfo::szDeviceName, 33, lpMONITORINFOEX->szDevice); SingleMonitorInfo::setStringMonitorNameAndDescription(lpMONITORINFOEX->szDevice); } I am rewriting my program using member

How to pass an enum to a parent class's static function to instantiate a child class?

╄→尐↘猪︶ㄣ 提交于 2019-12-12 03:32:49
问题 I have a 3 classes, ParentClass , ClassA , ClassB . Both ClassA and ClassB are subclasses of ParentClass . I wanna try to create objects of type ClassA or ClassB using some kind of enumeration to identify a type, and then instantiate the object cast as the parent type. How can I do that dynamically? Please take a look at the code below, and the parts that say //what do I put here? . Thanks for reading! enum ClassType { ClassA, ClassB }; public abstract class ParentClass { public ParentClass()

Subclassing `frozenset` with multiple `__init__` parameters, and strange behaviour of `__new__`

牧云@^-^@ 提交于 2019-12-12 03:27:39
问题 Before you mark this a duplicate, I have read this question and this question and they don't answer my question (and even seem to confuse me more, not to mention that they're both in Python 2 and I want to know about Python 3). From this question, understand why the following code, from typing import Hashable class UnorderedPair(frozenset): def __init__(self, left: Hashable, right: Hashable): self.left = left self.right = right super().__init__((left, right)) if len(self) != 2: raise