constructor

order of constructors and destructors

扶醉桌前 提交于 2019-12-25 18:53:29
问题 Here is a piece of code : #include<iostream> using namespace std; class cls { int x; public: cls(int i=0) {cout<<" c1 "; x=i;} ~cls() {cout<<" d 1 ";} }; class cls1 { int x; cls xx; public: cls1(int i=0){cout<<" c2 ";x=i;} ~cls1(){cout<<" d2 ";} }c; class cls2 { int x;cls1 xx;cls xxx; public: cls2(int i=0) {cout<<" c3 ";x=i;} ~cls2(){ cout<<" d3 ";} }; int main() { cls2 s; return 0; } The output is c1 c2 c1 c2 c1 c3 d3 d1 d2 d1 d2 d1 and I do not understand why . I would need some help. 回答1:

Getting error: A field initializer cannot reference the non-static field, method, or property

时光怂恿深爱的人放手 提交于 2019-12-25 18:33:47
问题 The error message was : Error 1 A field initializer cannot reference the non-static field, method, or property 'AmazingPaintball.Form1.thePoint' This is the constructor: namespace AmazingPaintball { class Paintball { public Point startPoint; public Paintball(Point myPoint) { startPoint = myPoint; } This is the code that causes the error: Point thePoint = new Point(50, 50); Paintball gun = new Paintball(thePoint); 回答1: You haven't shown enough context, but I suspect you've got something like:

Getting error: A field initializer cannot reference the non-static field, method, or property

本小妞迷上赌 提交于 2019-12-25 18:33:32
问题 The error message was : Error 1 A field initializer cannot reference the non-static field, method, or property 'AmazingPaintball.Form1.thePoint' This is the constructor: namespace AmazingPaintball { class Paintball { public Point startPoint; public Paintball(Point myPoint) { startPoint = myPoint; } This is the code that causes the error: Point thePoint = new Point(50, 50); Paintball gun = new Paintball(thePoint); 回答1: You haven't shown enough context, but I suspect you've got something like:

How to call constructor and variables from another class?

好久不见. 提交于 2019-12-25 18:29:29
问题 I have a main class: class Sportist{ private: string ime; int godina_na_ragjanje; int godisna_zarabotuvacka_EUR; public: Sportist(string i, int g_n_r, int g_z_EUR){ ime = i; godina_na_ragjanje = g_n_r; godisna_zarabotuvacka_EUR = g_z_EUR; } }; And now I have a new class like this: class Fudbaler:public Sportist{ private: int broj_na_odigrani_natprevari; int danocna_stapka; public: Fudbaler(string ime, int godina, int zarabotuvacka, int b, int d){ :Sportist(ime, godina, zarabotuvacka) broj_na

return properties from object using constructor function

假如想象 提交于 2019-12-25 18:14:14
问题 I have a constructor object that is created from a function, which is the response of a axios get request. I then want to return properties of that object and save some of the values to be used as a string in another class. eg: I want to be able to save values: response.data.name , response.data.address from the resposne. So far, im getting undefined when trying to get those values. export class MyClass { private _myobject: any; constructor(myobject: any) { this._myobject = myobject; } public

Properties shared between child and parents class in php

喜夏-厌秋 提交于 2019-12-25 17:20:59
问题 class parents{ public $a; function __construct(){ echo $this->a; } } class child extends parents{ function __construct(){ $this->a = 1; parent::__construct(); } } $new = new child();//print 1 This code above print 1,which means whenever we create an instance of a child class,and assign value to properties inherited from its parent,the property in its parent class also has been assigned.But the code below shows different: class parents{ public $a; function test(){ $child = new child(); echo

is it possible to define the static member function of a class in .cpp file instead of its header file?

霸气de小男生 提交于 2019-12-25 17:05:37
问题 i am having a function which should be run only once for all instance of the class.i thought to use the static function calling method. all the web example shows that static function define in the Header file(inside the class) itself. my function is big one i cant define that in header file what should i do? for that. 回答1: Like you do for normal functions: FooBar.h #ifndef FOOBAR_H #define FOOBAR_H class FooBar { public: static void test(); }; #endif FooBar.cpp #include "FooBar.h" void FooBar

Passing listBox to a new form

痴心易碎 提交于 2019-12-25 17:02:01
问题 I have a problem with my constructor. I got it set up like this: Form1: private void button10_Click(object sender, EventArgs e) { var form2 = new Form2(listBox1); form2.Show(); this.Hide(); } Form2: public Form2(ListBox listBox) { InitializeComponent(); listBox1.Items=listBox.Items; } I just want to send my data from listBox on the Form1 to my Form2 listBox but it's giving me this error: Property or indexer 'System.Windows.Forms.ListBox.Items' cannot be assigned to -- it is read only. 回答1:

Binary Tree type constructor in Haskell

廉价感情. 提交于 2019-12-25 16:56:00
问题 I'm trying binary tree type constructor which is: data Tree a = Leaf a | Branch a (Tree a) (Tree a) How we prove that not all kinds of binary tree can be represented by this constructor? How we improve this definition to cover all types of binary tree? And how it works? 回答1: Your Tree a has labels of type a at every Branch and every Leaf constructor. So, for example, Branch 'u' (Branch 'n' (Leaf 'i') (Leaf 'p')) (Leaf 'z') looks like this: +-'u'-+ | | +-'n'-+ 'z' | | 'i' 'p' That excludes,

Overloaded constructors

帅比萌擦擦* 提交于 2019-12-25 16:24:27
问题 After reading overloaded constructors from a book, i tired the following code: public class Employee { String name; int idNumber; public Employee(){ this("JJ", 0); System.out.println(name +" "+ idNumber); } public Employee(String name, int num){ this.name = name; idNumber = num; System.out.println(name +" 2nd "+ idNumber); } } public class Object1 { public static void main(String[] args) { Employee emp = new Employee(); } } OUTPUT: JJ 2nd 0 JJ 0 I am really confused. Why "JJ 2nd 0" printed