constructor

Does a type require a default constructor in order to declare an array of it?

痞子三分冷 提交于 2019-12-10 18:29:38
问题 I noticed that when you declare an array, the default constructor must be needed. Is that right? Is there any exception? For example, struct Foo{ Foo(int i ) {} }; int main () { Foo f[5]; return 0; } The code above does not compile. 回答1: Other answers are all right but, for completeness: You could also use the array initialization syntax: Foo f[5] = {1,2,3,4,5}; This works if Foo's ctor is not explicit. If it was, you'd have to be.... explicit: Foo f[5] = {Foo(1), Foo(2), Foo(3), Foo(4), Foo

Why “an inherited constructor is not a candidate for initialization from an expression of the same or derived type”?

亡梦爱人 提交于 2019-12-10 18:29:16
问题 I expected this to work struct Parent { Parent (); Parent (const Parent &); }; struct Child : public Parent { using Parent::Parent; }; Parent p; Child c (p); Child inherits all of Parent 's constructors, right? Including Parent::Parent(const Parent &) ? x.cpp:15:11: error: no matching function for call to ‘Child::Child(Parent&)’ Child c (p); ^ x.cpp:5:2: note: candidate: Parent::Parent(const Parent&) Parent (const Parent &); ^~~~~~ x.cpp:10:16: note: inherited here using Parent::Parent; ^~~~~

Error: cannot bind non-const lvalue reference of type ‘int&’ to an rvalue of type ‘int’

▼魔方 西西 提交于 2019-12-10 18:27:42
问题 I need to create a Bar object, which has a private object Foo f . However, the value of Foo object parameter should be passed by the specific method int genValue() . If I initialize f in the constructor scope Bar(){...} , the compiler yell error, something like there is no constructor Foo() . If I construct like this Bar(): f(genValue()) , the compiler yells the error: test.cpp: In constructor ‘Bar::Bar()’: test.cpp:16:19: error: cannot bind non-const lvalue reference of type ‘int&’ to an

When using a JsonWriter, what's the purpose of WriteStartConstructor?

£可爱£侵袭症+ 提交于 2019-12-10 18:26:20
问题 Title says it all. I see it (with its corresponding end) spits out the following... new Foo( ) ...but I don't understand what the new actually does when deserializing. The docs just say it writes a Json constructor, but not what a Json constructor is ! 回答1: This method was introduced as part of an enhancement to allow Json.NET to parse date literals when represented with JavaScript constructors like so: new Date(1234656000000). For details, see Serializing Dates in JSON. Even though this

Linux vs Windows std::map assignment constructors (Why such a difference?)

僤鯓⒐⒋嵵緔 提交于 2019-12-10 18:25:07
问题 I was witnessing some unexpected behavior in a C++ application I am writing in Linux Ubuntu. I would construct an object with parameters and then put a copy of that object into a std::map using the assignment operator. I wrote a simple program to demonstrate this situation... #include <iostream> #include <string> #include <map> using namespace std; class Foo { public: Foo(void) : _x(0) { cout << "Default" << endl; } Foo(int a) : _x(a) { cout << "Param" << endl; } Foo(Foo const &foo) : _x(foo.

C++ error C2533, ctor : constructors not allowed a return type [closed]

杀马特。学长 韩版系。学妹 提交于 2019-12-10 18:23:01
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 3 years ago . I have a class called teacher class Teacher { private: int ID; string qualification; double salary; Date DOB; Date dateJoined; public: Teacher(); void setTeacher (int, string, double); string getQualification(); void displayTeacher(); } //This is my constructor Teacher::Teacher() { ID = 0; qualification =" " ;

Create child object in parent constructor

别来无恙 提交于 2019-12-10 18:17:59
问题 Is it possible to create an instance of a child object using the parent's constructor and passing it some kind of parameter (a string, in this case) to tell it the type of child that should be created? 回答1: No. In C#, you create an instance of a class, then the runtime calls its constructor. By the time the constructor executes it's too late to pick another type. However, a derived class' constructor always calls one of its base class' constructors, and you can use that to your advantage (to

Finding which member in an initializer list threw an exception

此生再无相见时 提交于 2019-12-10 17:48:16
问题 Lets say I have a class holding some members of some type. I know that the syntax for try-catch blocks with initializer lists is as follows template<int N> struct Member { Member() { std::cout << "Default constructor of Member " << N << std::endl; } }; class A { Member<1> m1; Member<2> m2; // n members public: A() try : m1() , m2() { // constructor implementation } catch (std::exception const & e) { // ... cleanup throw; // and rethrow } }; Since the initializer list is involved I can't use

Declaring a member variable that takes a constructor parameter

亡梦爱人 提交于 2019-12-10 17:44:41
问题 // In A.h class A { public: enum eMyEnum{ eOne, eTwo, eThree }; public: A(eMyEnum e); } // In B.h #include "A.h" class B { B(); private: A memberA; } // In B.cpp #include "B.h" B::B(void) : memberA(A::eOne) {} The declaration to 'memberA' gives me a compile error using the g++ compiler: error: 'A::eOne' is not a type How can I overcome this? Do I simply need to create a default constructor that takes no parameters? 回答1: It sounds like you are trying to initialise a member variable. You could

Multiple assertions when unit testing constructor?

混江龙づ霸主 提交于 2019-12-10 17:19:13
问题 I'm trying to unit test a class with 2 constructors. Each constructor has multiple parameters that set public properties. My question is, should I have only 2 unit tests with multiple asserts to check that each property was set OR a test for each parameter for each constructor? Public Person(string name, string phone, string birthday) { name = name; phone = phone; birthday = birthday; } Public Person(string name) : this(name, null, null) {} 回答1: The operation that you are testing is that the