constructor

Initialization of vector in a constructor - C++

烂漫一生 提交于 2019-12-13 07:40:20
问题 I'm struggling with the constructor of one of my classes do to a member that is not initialized properly. I have a class "Settings" that handles the setting I use for my simulations and a class Simulations that performs the simulation steps. What I can't understand is why this code doesn't work as expected: class Settings{ public: int n ; // a number I need to create properly a vector in my class simulation // ... rest of the code constructors etc to read values from files. // everything

Java: using generic wildcards with subclassing

…衆ロ難τιáo~ 提交于 2019-12-13 07:03:51
问题 Say I have a class Foo, a class A and some subclass B of A. Foo accepts A and its sublclasses as the generic type. A and B both require a Foo instance in their constructor. I want A's Foo to be of type A , and B's Foo to be of type B or a superclass of B. So in effect, So I only want this: Foo<X> bar = new Foo<X>; new B(bar); to be possible if X is either A, B, or a both subclass of A and superclass of B. So far this is what I have: class Foo<? extends A>{ //construct } class A(Foo<A> bar){ /

Call order of constructors

时光毁灭记忆、已成空白 提交于 2019-12-13 06:26:23
问题 #include <iostream> using namespace std; struct A{ A() {cout << "A" << endl;} A(int a) {cout << "A+" << endl;} }; struct B : virtual A{ B() : A(1) {cout << "B" << endl;} }; struct C : virtual A{ C() : A(1) {cout << "C" << endl;} }; struct D : virtual A{ D() : A() {cout << "D" << endl;} }; struct E : B, virtual C, D{ E(){cout << "E" << endl;} }; struct F : D, virtual C{ F(){cout << "F" << endl;} }; struct G : E, F{ G() {cout << "G" << endl;} }; int main(){ G g; return 0; } Program prints: A C

Why is object.constructor a “Function”, and not “newable” in TypeScript?

Deadly 提交于 2019-12-13 06:21:33
问题 In TypeScript, I usually define the type of a class type with: declare type Type = { new (...args: any[]): any } For example, this can used when a class is passed as an argument. This is kind-of similar to what Type is in C#, but it can also be instantiated directly with the new operator. AFAIK, this type definition is effectively analogous to: A constructor function that may receive any number and type of arguments, and may return anything. If an argument, property, or other variable is

Constructor with Optional<String>

大城市里の小女人 提交于 2019-12-13 05:58:12
问题 I have written the following code: void Test(A a) { B b = new B(a.getName()); } So, the constructor of B expects a String . It looks like the following: protected B (String name) { super(name, KIND); this.name = name; } But a.getName() gives me a name with Optional<String> as return value and I do not want to change that. Therefore, I try to change the parameter of the constructor B (I replace String name with Optional<String> ), but then Eclipse underlines super(name, KIND) and this.name =

Visual C++ 2010 : unordered_map and move constructor

纵然是瞬间 提交于 2019-12-13 05:56:52
问题 I have a structure named Foo which contains a unique_ptr struct Foo { std::unique_ptr<Bar> pointer; }; Now I'm trying to store instances of Foo in an unordered_map std::unordered_map<int,Foo> myMap; Technically this should be possible, since maps do not require a copy constructor, only a move constructor. However, I can't insert an element in my map: myMap.insert(std::make_pair(3, Foo())); This line will generate the following error in Visual C++ 2010 (roughly translated by me, since my

Is it possible to create a completely arbitrary private member tuple in a C++11 variadic class constructor?

大兔子大兔子 提交于 2019-12-13 05:50:22
问题 My apologies if this has been asked before - searched with no definite answer, and I'm beginning to wonder if it is even possible. I am trying to learn C++11 and have run into trouble with variadic templates. I think I grasp (finally) the concept of variadic function parameters, and why/how recursion is used to unwrap and process them, but am having trouble with a (I think) similar concept in class constructors. Suppose I want to create a variadic template class that has a mixed-type

Construction of object with non-default constructor inside C++ class

我与影子孤独终老i 提交于 2019-12-13 05:25:35
问题 Iam new to C++ and below mentioned is the summary of the problem. Bar's constructor needs to explicitly call foo's constructor and the argument to foo's constructor has to be an object to baz, which has a default constructor. Iam not allowed to use new operator(dynamic allocation) to achieve this. I tried the below code, but C++ compiler gives me compilation errors (listed below). Can somebody please explain me what's going wrong in this code? Any help is really appreciated. //Constructor.cpp

C++ Error C2512: no appropriate default constructor available

心不动则不痛 提交于 2019-12-13 05:18:27
问题 I have a class called MainWindow without a default constructor. I have a class called Application , its constructor uses an instance of MainWindow as a parameter. I get an Error C2512, "no appropriate default constructor available" in the definition of the constructor from the class Application. Here's the code of the constructor: Application::Application(HINSTANCE hInstance, MainWindow mainWindow) {...} I'm creating the instance of Application like this: MainWindow window(1000, 1000, false,

Do default constructors need to call base class default constructors?

好久不见. 提交于 2019-12-13 05:17:58
问题 Was reading this answer and it surprised me, the suggestion you must always call a base class constructor in the derived class constructor. What if you are working only with default constructors, consider: class Bar : public Foo { private: int y; public: Bar() : Foo(), y(0) { } ... Is the call to Foo() really necessary here? 回答1: You do not need to explicitly call a base class constructor in your constructor, in which case the compiler implicitly calls the default constructor, or you get a