initialization-list

“Middle classes” in diamond inheritance graph using non-default virtual base constructor: why is it not a compile error?

偶尔善良 提交于 2021-02-08 09:58:08
问题 Consider a diamond inheritance graph (i.e., virtual base class). We know from previous questions that on construction the most derived class directly calls the default (0-arg) constructor of the (virtual) base. But we also know from answers to the previous question (e.g., here that if the "middle" classes in the diamond have constructors that are used by the most-derived class and those constructors "call" non-default constructors of their (virtual) base class (via the initialization list)

“Middle classes” in diamond inheritance graph using non-default virtual base constructor: why is it not a compile error?

南笙酒味 提交于 2021-02-08 09:56:53
问题 Consider a diamond inheritance graph (i.e., virtual base class). We know from previous questions that on construction the most derived class directly calls the default (0-arg) constructor of the (virtual) base. But we also know from answers to the previous question (e.g., here that if the "middle" classes in the diamond have constructors that are used by the most-derived class and those constructors "call" non-default constructors of their (virtual) base class (via the initialization list)

How to let a variable be dependent on other variables inside a class?

天涯浪子 提交于 2021-01-27 04:48:23
问题 What is wrong with the variable international_standard_book_number ? How can I make it that it changes, whenever isbn_field_i changes? #include <iostream> #include <string> class ISBN { private: unsigned int isbn_field_1 = 0; unsigned int isbn_field_2 = 0; unsigned int isbn_field_3 = 0; char digit_or_letter = 'a'; std::string international_standard_book_number = std::to_string(isbn_field_1) + "-" + std::to_string(isbn_field_2) + "-" + std::to_string(isbn_field_3) + "-" + digit_or_letter;

Initializing (list) properties in constructor using reflection

谁说胖子不能爱 提交于 2020-04-14 03:37:13
问题 I am trying to initialize all properties in class (lists) with using reflection: public class EntitiesContainer { public IEnumerable<Address> Addresses { get; set; } public IEnumerable<Person> People { get; set; } public IEnumerable<Contract> Contracts { get; set; } public EntitiesContainer() { var propertyInfo = this.GetType().GetProperties(); foreach (var property in propertyInfo) { property.SetValue(property, Activator.CreateInstance(property.GetType()), null); } } } I am getting exception

Member initializer list: initialize two members from a function returning a tuple

本小妞迷上赌 提交于 2020-02-29 18:12:09
问题 Can multiple members be initialized in the member initializer list from a tuple obtained by a function? With returning multiple values via tuples becoming more popular I hope there is a solution for this. I see no reason other than a language limitation why this would not be possible. This is a mcve for what I have: auto new_foo(std::size_t size) -> std::tuple<std::unique_ptr<char[]>, int*> { auto buffer = std::make_unique<char[]>(size * sizeof(int) + 8); auto begin = static_cast<int*>(static

Is std::move really needed on initialization list of constructor for heavy members passed by value?

我与影子孤独终老i 提交于 2019-12-31 08:36:09
问题 Recently I read an example from cppreference.../vector/emplace_back: struct President { std::string name; std::string country; int year; President(std::string p_name, std::string p_country, int p_year) : name(std::move(p_name)), country(std::move(p_country)), year(p_year) { std::cout << "I am being constructed.\n"; } My question: is this std::move really needed? My point is that this p_name is not used in the body of constructor, so, maybe, there is some rule in the language to use move

Is there a Way to Get Warned about Misbehaving Designated Initializers?

混江龙づ霸主 提交于 2019-12-30 10:56:09
问题 C99 introduced the concept of designated intializers for structs. So for example, given: typedef struct { int c; char a; float b; } X; I could initialize like: X foo = {.a = '\1', .b = 2.0F, .c = 4}; and calling: printf("c = %d\na = %hhu\nb = %f", foo.c, foo.a, foo.b); would output: c = 4 a = 1 b = 2.000000 As mentioned here this has the "surprising behavior" of assigning to c then a then b , independent of the order of my designated initializers. This becomes a real issue if I have functions

Order of calling base class constructor from derived class initialization list

限于喜欢 提交于 2019-12-28 04:14:06
问题 struct B { int b1, b2; B(int, int); }; struct D : B { int d1, d2; // which is technically better ? D (int i, int j, int k, int l) : B(i,j), d1(k), d2(l) {} // 1st Base // or D (int i, int j, int k, int l) : d1(k), d2(l), B(i,j) {} // last Base }; Above is just pseudo code. In actual I wanted to know that does the order of calling base constructor matter ? Are there any bad behaviors (especially corner cases ) caused by any of the cases ? My question is on more technical aspect and not on

C++ Class Initialization List example

北战南征 提交于 2019-12-23 02:18:09
问题 I am going through Chapter 17 in the new Stroustrup book and I am confused by initializing a class with an initialization list. Example: in .hpp: class A { public: A() : _plantName(std::string s), _growTimeMinutes(int 1); virtual ~A(); private: std::string _plantName; int _growTimeMinutes; }; in .cpp: A::A() : _plantName(std::string s), _growTimeMinutes(int i) { } or is it in .cpp: A::A(std::string s, int i) : _plantName(std::string s), _growTimeMinutes(int i) { } and calling that: A a {

How can i initialize superclass params from within the child c-tor in C++?

与世无争的帅哥 提交于 2019-12-22 14:45:12
问题 Watch the following example: class A { public: A(int param1, int param2, int param3) { // ... } }; class B : public A { public: B() : m_param1(1), m_param(2), m_param(3), A(m_param1, m_param2, m_param3) { // ... } }; B b; Obviously, when "b" will be created, A's ctor will be called before the parameters of B will be initialized. This rule prevents me from creating "wrapper" classes which simplify the class's initialization. What is the "right way" for doing it? Thanks, Amir PS: In my