default-constructor

How can a compiler generated default constructor be more efficient than a self-written one that does nothing but initialize members? [duplicate]

你离开我真会死。 提交于 2020-01-03 11:32:07
问题 This question already has answers here : Why would the implicitly generated constructor (et al.) be more efficient than a user-defined (trivial) one? (4 answers) Are user-defined default constructors less efficient? (2 answers) Closed 7 months ago . Triggered by this answer I was reading in the core guidelines: C.45: Don’t define a default constructor that only initializes data members; use in-class member initializers instead The reasoning given is Reason Using in-class member initializers

How can a compiler generated default constructor be more efficient than a self-written one that does nothing but initialize members? [duplicate]

柔情痞子 提交于 2020-01-03 11:31:52
问题 This question already has answers here : Why would the implicitly generated constructor (et al.) be more efficient than a user-defined (trivial) one? (4 answers) Are user-defined default constructors less efficient? (2 answers) Closed 7 months ago . Triggered by this answer I was reading in the core guidelines: C.45: Don’t define a default constructor that only initializes data members; use in-class member initializers instead The reasoning given is Reason Using in-class member initializers

Trouble overriding save_construct_data when serializing a pointer to a class without a default constructor

爱⌒轻易说出口 提交于 2020-01-02 08:12:32
问题 I'm trying to follow this example http://www.boost.org/doc/libs/1_42_0/libs/serialization/doc/serialization.html#constructors but I keep getting errors. Following the example, I get an error trying to access a private variable (fair enough): bs.cpp:10: error: ‘const int my_class::m_attribute’ is private But, if I add save_construct_data as a friend, I get an ambiguity error: /usr/include/boost/serialization/serialization.hpp:148: error: call of overloaded ‘save_construct_data(boost::archive:

Cython and constructors of classes

六月ゝ 毕业季﹏ 提交于 2020-01-01 17:27:06
问题 I have a problem with Cython usage of default constructors. My C++ class Node is the following Node.h class Node { public: Node() { std::cerr << "calling no arg constructor" << std::endl; w=0.0; d=0.0; } Node(double val, double val2); { std::cerr << "calling 2 args constructor" << std::endl; this->w=val; this->d=val2; } private: double d,w; } is wrapped in Cython as follows cdef extern from "Node.h": cdef cppclass Node: Node() except + Node(double val1, double val2) except + double d double w

What's the point of deleting default class constructor?

最后都变了- 提交于 2019-12-31 09:07:08
问题 I'm preparing for my CPP exam and one of the question is: Can you delete default class constructor and if so, what would be the reason to do so? OK, so obviously you can do it: class MyClass { public: MyClass() = delete; }; but I do not understand why would you do it? 回答1: Consider the following class: struct Foo { int i; }; This class is an aggregate, and you can create an instance with all three of these definitions: int main() { Foo f1; // i uninitialized Foo f2{}; // i initialized to zero

uninitialized const

余生颓废 提交于 2019-12-29 04:15:31
问题 This compiles perfectly fine with the current MSVC compiler: struct Foo { } const foo; However, it fails to compile with the current g++ compiler: error: uninitialized const 'foo' [-fpermissive] note: 'const struct Foo' has no user-provided default constructor If I provide a default constructor myself, it works: struct Foo { Foo() {} } const foo; Is this another case of MSVC being too permissive, or is g++ too strict here? 回答1: The C++03 Standard: 8.5 [dcl.init] paragraph 9 If no initializer

Does Spring require all beans to have a default constructor?

社会主义新天地 提交于 2019-12-28 06:27:30
问题 I don't want to create a default constructor for my auditRecord class. But Spring seems to insist on it: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'auditRecord' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.bartholem.AuditRecord]: No default constructor found; nested exception is java

Why is super class constructor always called [duplicate]

◇◆丶佛笑我妖孽 提交于 2019-12-28 03:04:29
问题 This question already has answers here : Why do this() and super() have to be the first statement in a constructor? (19 answers) Closed 3 years ago . I have the following 2 classes public class classA { classA() { System.out.println("A"); } } class classB extends classA { classB() { System.out.println("B"); } } and then running 1 classA c = new classB(); or 2 classB c = new classB(); always gives A B Why is this happening? At first glance, in either scenario, I would assume that only the

Does int() return 0 or an arbitrary value?

北城余情 提交于 2019-12-24 13:43:04
问题 Consider this code: template <typename T> void f() {T x = T();} When T = int , is x equal to 0 or to an arbitrary value? Bonus question: and consequently, are arrays (both T[N] and std::array<T, N> ) the only types where such a syntax may leave contents with arbitrary values. 回答1: T() gives value initialization , which gives zero-initialization for a type other than a class, union or array. (§8.5/7 bullet 3): "otherwise, the object is zero-initialized." For an array, each element of the array

Copy constructor for class that has member without default constructor in C++

淺唱寂寞╮ 提交于 2019-12-24 12:27:15
问题 I have a class: class Geometry{ std::vector<Subset *> subsets; int verticesCount; ... }; I want to add a copy constructor, so I can make a deep copy of that object (with own subsets , NOT only own pointers to same subsets ). I have tried this: Geometry & Geometry::operator=(const Geometry &other){ verticesCount = other.verticesCount; Subset * subset = 0; for(unsigned int i=0; i<other.subsets.size(); i++){ subset = new Subset(); subsets.push_back(subset); } ... return *this; } The problem is