initialization

Initializing field outside __init__

三世轮回 提交于 2021-02-10 03:09:23
问题 I need a bit of help to understand how python initialising works. I have a class (Bar) with another class (Foo) as a field/variable. When I try to initialise this variable directly in Bar (not in the class __init__) all instances of Bar will point to the same Foo. But if I have an __init__ method, as in Bar2, each Bar2 instance will have a unique Foo instance. What is happening here? class Foo(): number = 0 class Bar(): foo = Foo() class Bar2(): foo = None def __init__(self): self.foo = Foo()

Static variable shadowing global

为君一笑 提交于 2021-02-09 11:13:15
问题 I am trying to create an object using placement new (I know to use smart pointers, this is just to learn). My code is as follows: #include <vector> #include <iostream> #include <memory> using namespace std; // please excuse this // if you change like 19 to arr1 (or any other var name) instead of arr and line 40 to arr1 then it works struct A { int in = 999; A() {cout << "A ctor\n";} ~A() {cout << "A dtor\n";} }; char arr[sizeof(A)]; class B { public: static char arr[sizeof(A)]; const static A

Static variable shadowing global

纵饮孤独 提交于 2021-02-09 11:12:24
问题 I am trying to create an object using placement new (I know to use smart pointers, this is just to learn). My code is as follows: #include <vector> #include <iostream> #include <memory> using namespace std; // please excuse this // if you change like 19 to arr1 (or any other var name) instead of arr and line 40 to arr1 then it works struct A { int in = 999; A() {cout << "A ctor\n";} ~A() {cout << "A dtor\n";} }; char arr[sizeof(A)]; class B { public: static char arr[sizeof(A)]; const static A

Initializing member class with non-default constructor

风流意气都作罢 提交于 2021-02-09 01:33:06
问题 I'm trying to make a gui which has a SimpleWindow class that contains a textPanel class: class textPanel{ private: std::string text_m; public: textPanel(std::string str):text_m(str){} ~textPanel(); }; class SimpleWindow{ public: SimpleWindow(); ~SimpleWindow(); textPanel text_panel_m; }; SimpleWindow::SimpleWindow(): text_panel_m(std::string temp("default value")) { } I want to be able to initialize the text_panel_m using a const char* that gets converted to a std::string without needing to

Initializing member class with non-default constructor

社会主义新天地 提交于 2021-02-09 01:29:11
问题 I'm trying to make a gui which has a SimpleWindow class that contains a textPanel class: class textPanel{ private: std::string text_m; public: textPanel(std::string str):text_m(str){} ~textPanel(); }; class SimpleWindow{ public: SimpleWindow(); ~SimpleWindow(); textPanel text_panel_m; }; SimpleWindow::SimpleWindow(): text_panel_m(std::string temp("default value")) { } I want to be able to initialize the text_panel_m using a const char* that gets converted to a std::string without needing to

Default value of a struct member

三世轮回 提交于 2021-02-08 21:41:49
问题 (I'm sure this question has already been answered, I'm just not sure on the right words to use to ask it. If someone would tell me what the correct terminology is that would be awesome!) I'm implementing a HashSet in C++ for a data structures class, and I have a question about C++ syntax regarding structs. Here is my code: struct HashNode { T value; HashNode* next = nullptr; }; Will this code correctly initialize the next pointer to nullptr when new HashNode is called? If not, what is the

braced-init-list and unsigned types

这一生的挚爱 提交于 2021-02-08 13:23:17
问题 gcc 8 and clang 7 do not accept the following code, which should default-construct a temporary of type unsigned int : unsigned int ui = unsigned int{}; clang 7 reports an error such as <source>:6:22: error: expected primary-expression before 'unsigned' Visual C++ 2015 and 2017 accept this. Obviously, this works with a type such as int , or any default-constructible class type. Is this correct C++14 code (and in that case a bug of clang and gcc)? If not, why not? Which types other than

braced-init-list and unsigned types

白昼怎懂夜的黑 提交于 2021-02-08 13:22:41
问题 gcc 8 and clang 7 do not accept the following code, which should default-construct a temporary of type unsigned int : unsigned int ui = unsigned int{}; clang 7 reports an error such as <source>:6:22: error: expected primary-expression before 'unsigned' Visual C++ 2015 and 2017 accept this. Obviously, this works with a type such as int , or any default-constructible class type. Is this correct C++14 code (and in that case a bug of clang and gcc)? If not, why not? Which types other than

Creating an array of two-dimensional arrays in C#

三世轮回 提交于 2021-02-08 12:21:37
问题 I simply want to create an array of two dimensional arrays to store coordinate points. So I want an array where each index returns a two dimensional array which I would use as x and y . Here's what I've tried: waypoints = new int[4][,] { {{0},{6, 0}}, {{1},{1, 1}}, {{2},{1, 5}}, {{3},{6, 5}} }; I realize this probably looks stupid, but I've tried looking it up on Google, and I have not gotten any good results. It gives an error: "error CS0623: Array initializers can only be used in a variable

Creating an array of two-dimensional arrays in C#

半腔热情 提交于 2021-02-08 12:21:07
问题 I simply want to create an array of two dimensional arrays to store coordinate points. So I want an array where each index returns a two dimensional array which I would use as x and y . Here's what I've tried: waypoints = new int[4][,] { {{0},{6, 0}}, {{1},{1, 1}}, {{2},{1, 5}}, {{3},{6, 5}} }; I realize this probably looks stupid, but I've tried looking it up on Google, and I have not gotten any good results. It gives an error: "error CS0623: Array initializers can only be used in a variable