default-constructor

Does virtual inheritance force a base class to be default constructible?

邮差的信 提交于 2021-02-07 13:17:11
问题 In the following code, the compiler is requesting the base class X to be default constructible . However, if I remove the virtual keyword from the inheritance of the class Node , the access to the member m_x becomes, of course, ambiguous, but the default constructor for class X is no longer required. What is the reason for that? #include <iostream> struct Apply { template< typename T > struct Node : virtual T // this line contains the virtual inheritance { template< typename ...Args> Node(

Does virtual inheritance force a base class to be default constructible?

让人想犯罪 __ 提交于 2021-02-07 13:16:33
问题 In the following code, the compiler is requesting the base class X to be default constructible . However, if I remove the virtual keyword from the inheritance of the class Node , the access to the member m_x becomes, of course, ambiguous, but the default constructor for class X is no longer required. What is the reason for that? #include <iostream> struct Apply { template< typename T > struct Node : virtual T // this line contains the virtual inheritance { template< typename ...Args> Node(

How to extend an abstract class in scala and use the abstract constructor

旧城冷巷雨未停 提交于 2021-01-29 04:10:25
问题 I have abstract class A abstract class A{ def this(obj:Object){ this() obj match{ case s:String => stringMethod(s) case n:Int => intMethod(n) } def stringMethod(s:String) def intMethod(n:Int) } and I have a class that extends this class class B(obj:Object) extends A(obj){ var s:String = null def stringMethod(s:String){ this.s = s } def intMethod(n:Int){ this.s = n.toString } } The point of this class is to instantiate an object and its variables depending on the class type of the object used

How to extend an abstract class in scala and use the abstract constructor

不问归期 提交于 2021-01-29 04:04:52
问题 I have abstract class A abstract class A{ def this(obj:Object){ this() obj match{ case s:String => stringMethod(s) case n:Int => intMethod(n) } def stringMethod(s:String) def intMethod(n:Int) } and I have a class that extends this class class B(obj:Object) extends A(obj){ var s:String = null def stringMethod(s:String){ this.s = s } def intMethod(n:Int){ this.s = n.toString } } The point of this class is to instantiate an object and its variables depending on the class type of the object used

Multiple Default Constructors

╄→гoц情女王★ 提交于 2020-04-11 08:38:11
问题 From this stack overflow question the answer contains this quote: ... definition says that all default constructors (in case there are multiple) ... How can there be multiple default constructors, and why may that be useful or allowed by the standard? 回答1: Default constructors don't have to have no parameters; they just need to be invocable with no arguments. This condition is fulfilled by any constructor whose arguments all have defaults. [class.dtor/1]: A default constructor for a class X

Unclear most vexing parse [duplicate]

一曲冷凌霜 提交于 2020-02-25 10:14:22
问题 This question already has answers here : Why does printing a function name returns a value? (3 answers) Closed 7 months ago . Let's assume the following class Foo. struct Foo { int i; }; if I want to make an instance of this class and initialize it, I should do: Foo foo1 = Foo(); to call the constructor. int main(void) { foo1 = Foo(); cout << foo1.i << " : " << foo1.j << endl; // " 0 " } then I thought I'd call the default constructor with the following line but it doesn't: int main(void) {

Unclear most vexing parse [duplicate]

半城伤御伤魂 提交于 2020-02-25 10:14:09
问题 This question already has answers here : Why does printing a function name returns a value? (3 answers) Closed 7 months ago . Let's assume the following class Foo. struct Foo { int i; }; if I want to make an instance of this class and initialize it, I should do: Foo foo1 = Foo(); to call the constructor. int main(void) { foo1 = Foo(); cout << foo1.i << " : " << foo1.j << endl; // " 0 " } then I thought I'd call the default constructor with the following line but it doesn't: int main(void) {

How to utilize template copy&move constructor and assignment operator?

拜拜、爱过 提交于 2020-02-03 07:54:13
问题 Consider the following C++ code with my failed attempt to avoid preference of non-template copy&move constructors and assignment operators: template<typename T> class A { public: A() { /* implementation here */ } // Remove from the overloads the default copy&move constructors and assignment operators A(const A&) = delete; A& operator=(const A&) = delete; A(A&&) = delete; A& operator=(A&&) = delete; // I want these to be used e.g. by std::vector template<typename U> A(const A<U>& fellow) { /*

How to elegantly return an object that is default-initialized?

心已入冬 提交于 2020-01-29 06:53:50
问题 I have a class like below: class VeryVeryVeryLongTypeName { bool is_ok; VeryVeryVeryLongTypeName() : is_ok(false) {} }; VeryVeryVeryLongTypeName f() { VeryVeryVeryLongTypeName v; ... // Doing something if (condition_1 is true) { return v; } else { return VeryVeryVeryLongTypeName(); } ... // Doing something if (condition_2 is true) { return v; } else { return VeryVeryVeryLongTypeName(); } } I think the statement return VeryVeryVeryLongTypeName(); is very tedious and ugly, so, my question is: