initialization

C++ CRTP initialization

大憨熊 提交于 2020-04-28 20:44:19
问题 i ran into a segfault running the following program #include <iostream> #include <vector> template <typename Derived> struct CRTPBase { CRTPBase() { func(); } void func() { static_cast<Derived*>(this)->_func(); } }; struct CRTPChild : CRTPBase<CRTPChild>{ using CRTPBase<CRTPChild>::CRTPBase; void _func(){ vec.resize(10); vec[0] = 2; } std::vector<int> vec; }; int main() { CRTPChild obj; std::cout << obj.vec[0] << std::endl; } When i replace vec with a member of type int it doesn't segfault

C++ CRTP initialization

╄→尐↘猪︶ㄣ 提交于 2020-04-28 20:43:39
问题 i ran into a segfault running the following program #include <iostream> #include <vector> template <typename Derived> struct CRTPBase { CRTPBase() { func(); } void func() { static_cast<Derived*>(this)->_func(); } }; struct CRTPChild : CRTPBase<CRTPChild>{ using CRTPBase<CRTPChild>::CRTPBase; void _func(){ vec.resize(10); vec[0] = 2; } std::vector<int> vec; }; int main() { CRTPChild obj; std::cout << obj.vec[0] << std::endl; } When i replace vec with a member of type int it doesn't segfault

Does C++ zero-initialise unspecified values during aggregate initialization?

房东的猫 提交于 2020-04-18 06:12:33
问题 An interesting question arose as a side effect of some other question here, about the possible differences between how C and C++ handle (the non-static-storage-duration): int arr[7] = {0}; Someone was stating that, in C++, the other elements were not guaranteed to be zero but I'm not sure I agree. Now C11 states, in 6.7.9 Initialization /19 : The initialization shall occur in initializer list order, each initializer provided for a particular subobject overriding any previously listed

Missing example of a Member Initializer List on p. 184 of Programming Principles and Practice Using C++, 2nd ed

≡放荡痞女 提交于 2020-04-17 21:25:07
问题 I'm currently having trouble with part of Chapter 6 of "Programming: Principles and Practice Using C++" (2nd ed, 3rd printing). According to the book's index, an example of a member initializer list is on page 184. Part of page 184 reads as follows: "Here, we'll just provide two member functions to give us a more convenient way of initializing Tokens: class Token { public: char kind; // what kind of token double value; // for numbers: a value }; We can now initialize ("construct") Token

Why do we not need to include self in super()?

╄→гoц情女王★ 提交于 2020-04-11 08:31:27
问题 Why in super() keyword, can we omit self ? What if we don't omit it? class A: def __init__(self, var1, var2): """ some code """ class B(A): def __init__(self, varA, varB): super().__init__(varA, varB) """ some code """ 回答1: class Rectangle: def __init__(self, length, width): self.length = length self.width = width def area(self): return self.length * self.width def perimeter(self): return 2 * self.length + 2 * self.width class Square(Rectangle): def __init__(self, length): super(Square, self)

C++ Segmentation Fault when using cout in static variable initialization

雨燕双飞 提交于 2020-04-09 06:49:28
问题 I have a program where I use cout to emit debug information. The code is executed in the initialization of a static global variable, i.e. quite early in the program execution. When I use my own build script to build the program, it segfaults at the first use of cout (only a string literal is shifted into cout, so it cannot be the value). I used valgrind to check for earlier writes to invalid locations, but there are none (and there is also no code that would be likely to generate those writes

C++ Segmentation Fault when using cout in static variable initialization

怎甘沉沦 提交于 2020-04-09 06:48:13
问题 I have a program where I use cout to emit debug information. The code is executed in the initialization of a static global variable, i.e. quite early in the program execution. When I use my own build script to build the program, it segfaults at the first use of cout (only a string literal is shifted into cout, so it cannot be the value). I used valgrind to check for earlier writes to invalid locations, but there are none (and there is also no code that would be likely to generate those writes

C++ Segmentation Fault when using cout in static variable initialization

老子叫甜甜 提交于 2020-04-09 06:47:53
问题 I have a program where I use cout to emit debug information. The code is executed in the initialization of a static global variable, i.e. quite early in the program execution. When I use my own build script to build the program, it segfaults at the first use of cout (only a string literal is shifted into cout, so it cannot be the value). I used valgrind to check for earlier writes to invalid locations, but there are none (and there is also no code that would be likely to generate those writes

C++ Segmentation Fault when using cout in static variable initialization

﹥>﹥吖頭↗ 提交于 2020-04-09 06:44:45
问题 I have a program where I use cout to emit debug information. The code is executed in the initialization of a static global variable, i.e. quite early in the program execution. When I use my own build script to build the program, it segfaults at the first use of cout (only a string literal is shifted into cout, so it cannot be the value). I used valgrind to check for earlier writes to invalid locations, but there are none (and there is also no code that would be likely to generate those writes

Double array initialization in Java

此生再无相见时 提交于 2020-03-21 10:56:26
问题 I was reading a book on Java and came across an example in which an array of type double was initialized in a way that I haven't seen before. What type of initialization is it and where else can it be used? double m[][]={ {0*0,1*0,2*0,3*0}, {0*1,1*1,2*1,3*1}, {0*2,1*2,2*2,3*2}, {0*3,1*3,2*3,3*3} }; 回答1: This is array initializer syntax , and it can only be used on the right-hand-side when declaring a variable of array type. Example: int[] x = {1,2,3,4}; String y = {"a","b","c"}; If you're not