constructor

c++ call superclass constructor with calculated arguments

梦想的初衷 提交于 2019-12-12 10:44:25
问题 probably it's super easy, but can someone tell me how I can call the superclass' constructor with arguments calculated in the subclass' constructor? something like this: class A{ A(int i, int j); }; class B : A{ B(int i); }; B::B(int i){ int complex_calculation_a= i*5; int complex_calculation_b= i+complex_calculation_a; A(complex_calculation_a, complex_calculation_b); } //edit: i edited the example so that the superclass takes two arguments which have a relation to each other 回答1: If you

Why would you use a class method constructor versus alloc/init?

此生再无相见时 提交于 2019-12-12 10:43:57
问题 There are two choices for constructors in Objective C/Cocoa: 1. Class Constructor Product *product = [Product productWithIdentifier:@"Chocolate"]; // Use product 2. Alloc/init Constructor Product *product = [[Product alloc] initWithIdentifier:@"Chocolate"]; // Use product [product release]; What I do I tend to use the class method just because it looks cleaner and you don't need a release. I see a lot of alloc/init - what's the advantage to doing it this way? My Question Which one is

C++/CLI, static constructor outside class declaration

▼魔方 西西 提交于 2019-12-12 10:33:02
问题 How do I put body of static constructor of a managed class outside class declaration? This syntax seems to be compilable, but does it really mean static constructor, or just a static (=not visible outside translation unit) function? ref class Foo { static Foo(); } static Foo::Foo() {} 回答1: Yes, that is the correct syntax to create a C++/CLI static constructor. You can know its not creating a static function since that is not a valid function declaration syntax. Functions must have the return

Javascript call Parent constructor in the Child (prototypical inheritance) - How it works?

对着背影说爱祢 提交于 2019-12-12 09:34:22
问题 I know it works, but I don't know why and how. What are the mechanics? // Parent constructor function Parent(name){ this.name = name || "The name property is empty"; } // Child constructor function Child(name){ this.name = name; } // Originaly, the Child "inherit" everything from the Parent, also the name property, but in this case // I shadowing that with the name property in the Child constructor. Child.prototype = new Parent(); // I want to this: if I dont set a name, please inherit "The

How to forbid the use of the default constructor in C++?

五迷三道 提交于 2019-12-12 09:33:32
问题 I don't want to make it possible in my program to create an object without passing arguments to the constructor. Is there a way? 回答1: When you declare any other constructor, the compiler will not generate the default constructor for you. If you have specified a default no-argument constructor, you can make it private. Remember that the compiler can automatically generate each of these 4 member functions for a class. default no argument constructor default destructor copy constructor

How to set base class members before derived class constructor is called?

巧了我就是萌 提交于 2019-12-12 09:26:21
问题 There is a base class for components of my application which provides some members and the functions Init() and Update() which must be overwritten. class Component { public: void Set(type* Member1, type* Member2, type* Member3) { this->Member1 = Member1; this->Member2 = Member2; this->Member3 = Member3; } virtual void Init() = 0; virtual void Update() = 0; virtual ~Component() {} protected: type* Member1; type* Member2; type* Member3; }; For handeling the components, there is a manager. It

Allocation of memory ( C++ ) Compile-time/Run-time?

纵饮孤独 提交于 2019-12-12 09:26:01
问题 I am not sure how appropriate is this question, but - I am curious about how the compiler sets memory aside for an object (allocation of memory) even before it is constructed (before even the constructor is called!) . How does it happen for primitive datatypes? This sounds a bit naive, but what exactly is it ? Is it entirely a run time process, or does it (the compiler) have any plans like to do this , to do that , during run-time, which it decides before hand during the compile- time . I

Member initialization of a data structure's members

安稳与你 提交于 2019-12-12 09:09:04
问题 I just ran into an awkward issue that has an easy fix, but not one that I enjoy doing. In my class's constructor I'm initializing the data members of a data member. Here is some code: class Button { private: // The attributes of the button SDL_Rect box; // The part of the button sprite sheet that will be shown SDL_Rect* clip; public: // Initialize the variables explicit Button(const int x, const int y, const int w, const int h) : box.x(x), box.y(y), box.w(w), box.h(h), clip(&clips[CLIP

Does Stack<> constructor reverse the stack when being initialized from other one?

試著忘記壹切 提交于 2019-12-12 08:50:18
问题 Here is the code: var s = new Stack<int>(); s.Push(1); s.Push(2); s.Push(3); s.Push(4); var ns = new Stack<int>(s); var nss = new Stack<int>(new Stack<int>(s)); and then let's see the result tbLog.Text += "s stack:"; while(s.Count > 0) { tbLog.Text += s.Pop() + ","; } tbLog.Text += Environment.NewLine; tbLog.Text += "ns stack:"; while (ns.Count > 0) { tbLog.Text += ns.Pop() + ","; } tbLog.Text += Environment.NewLine; tbLog.Text += "nss stack:"; while (nss.Count > 0) { tbLog.Text += nss.Pop()

Constructor using {a,b,c} as argument or what is {a,b,c} actually doing?

六月ゝ 毕业季﹏ 提交于 2019-12-12 08:49:03
问题 I know, that I can initialize data like this. int array[3] = { 1, 2, 3 }; or even int array[2][2] = { {1, 2}, {3, 4} }; I can also do it with std::vector std::vector<int> A = { 1, 2, 3 }; Let's say I want to write my own class: class my_class { std::vector< int > A; public: //pseudo code my_class(*x) { store x in A;} //with x={ {1, 2}, {3, 4} } // do something }; Is it possible to write such a constructor and how is it possible? What is this statement {{1, 2}, {3, 4}} actually doing? I always