constructor

Initializing std::array private member through constructor

邮差的信 提交于 2019-12-11 15:28:02
问题 I have for example the following code: #include <iostream> #include <array> class Base { public: Base() : mA(std::array<int,2>()) {} Base(std::array<int,2> arr) : mA(arr) {} Base(/* what to write here ??? */); private: std::array<int,2> mA; }; int main() { std::array<int,2> a = {423, 12}; // Works fine Base b(a); // Works fine Base c({10, 20}); // This is what I need. return 0; } How should I define constructor to allow initialization with as shown in the 3rd line inside "main" above? In

Segmentation fault during runtime of console code in C++

家住魔仙堡 提交于 2019-12-11 15:23:26
问题 I'm trying to make a simple console student details display system whereby there are 2 classes, class student(Base class) with member variables for name and registration number and class studentAthlete(Derived class) with sports type string. The code compiles sucessfully but on runtime only asks for student details but does not display the details as would be expected when I call the identify() function. On code blocks, the error might not show but on online compiles such as https://www

c# Concatenate static arrays inside static class

笑着哭i 提交于 2019-12-11 15:10:51
问题 I am not even sure how to call what I want to achieve but... probably the code will explain it. Basically, I would like to create the frames commands as combinations of some statically defined arrays. I would like to do something like this: Command = ConcatArrays(commandPart1, commandPart2, commandPart2) But it fails inside ConcatArrays as the list elements seem to be null. And to use externally like this: Frame.Frame1.Command The ConcatArrays I took it from here: https://stackoverflow.com/a

Overloaded parent class constructors. Wrong inizializer choice?

别来无恙 提交于 2019-12-11 14:56:46
问题 I would like to add a Child class to a pre-existing project having Parent already defined and declared. Parent class has got two constructors with initializer-list. This is my code, and it generates error C2668: 'Parent::Parent' : ambiguous call to overloaded function. Where is my error? Thanks @Mape for your snippet #include <stdio.h> class Parent { public: // Here a constructor with one default trailing argument, i.e. myInt, // myDouble is not initialised. This is correct, as once one

cannot appear in a constant expression

孤街浪徒 提交于 2019-12-11 14:55:55
问题 In the following c++ programm: class matrix { public: int n; double **x; matrix(int n) : n(n) { x=new double[n][n]; for (int i=0;i<n;i++) { for(int j=0;j<n;j++) { x[i][j]=0; } } } ... I get the following error: "'n' cannot appear in a constant-expression". Since im relatively new to cpp i dont really know why this error occurs (especially because i did almost the exact same thing with a class called vector and there it was no problem at all) and how to fix it. I would really appreciate any

Parametized Constructor Not an Copy one

房东的猫 提交于 2019-12-11 13:54:57
问题 Say we have a class A that contains as a member of the same class: Class A{ const A &a; } I want to create a parametized constructor that passed the value of that member, but I do not want to define the copy constructor of the class. A(const A& memberA): a(memberA) {} How could indicate the compiler such thing? Thanks 回答1: A constructor that can take just a reference to the class it constructs is a copy constructor, whether you want it to be one or not. Copy constructors are defined thus: A

Cannot set Web Service constructor parameter

你说的曾经没有我的故事 提交于 2019-12-11 13:47:15
问题 Question Back ground: I have a project consisting of a DLL, a web service and a test project. Currently I'm simply trying to set the web service constructor string parameter through the use of Unity. The issue & error: The following shows the constructor structure of the web service class 'VersionControlFacade': private string _url; public VersionControlService() { } public VersionControlService(string url) { _url = url; } If I simply pass the 'url' parameter as shown in the following code it

thread safe construction with attribute assignment

五迷三道 提交于 2019-12-11 13:45:37
问题 from https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/object-and-collection-initializers class Cat { // Auto-implemented properties. public int Age { get; set; } public string Name { get; set; } } Cat cat = new Cat { Age = 10, Name = "Fluffy" }; The object initializers syntax allows you to create an instance, and after that it assigns the newly created object, with its assigned properties, to the variable in the assignment. vs https://stackoverflow.com/a

chaining constructors in Java without throwing exceptions from the default constructor

微笑、不失礼 提交于 2019-12-11 13:19:18
问题 I've read this: Can I use throws in constructor? -- which gave me the right idea, and led me to one answer, but was not very explicit. I've also read several others, but could not find my answer. To recap what I've learned for context, essentially, this will not compile... public ExampleClass(String FileName) { this(new FileInputStream(FileName)); } public ExampleClass(FileInputStream FileStream) { DoSomethingToSetupBasedUponFileStream(FileStream); } ...because the FileInputStream constructor

Inherit constructors from template base class without repeating template arguments?

China☆狼群 提交于 2019-12-11 12:56:10
问题 How do I inherit constructors from a template base class without repeating the template arguments (and without using macros): For example, this does not work (using GCC 4.8): template <typename T> struct base {}; template <typename U> struct derived : base<U> { using base::base; }; It does work if I repeat the template arguments of the base class: template <typename T> struct base {}; template <typename U> struct derived : base<U> { using base<U>::base; }; The problem is that "U" might be