initialization

Cannot assign to value: 'self' is immutable

无人久伴 提交于 2020-01-24 10:30:27
问题 I am trying to return an instance from custom init in subclass of NSMutableURLRequest : class Request: NSMutableURLRequest { func initWith(endPoint:String, methodType:RequestType, body:RequestBody,headers: [String:String]?) { self = NSMutableURLRequest.init(url: URL.init(string: endPoint, relativeTo: URL?)) //return request } } But compiler does not allow to do the same and i get the error "Cannot assign to value: 'self' is immutable". What is the correct way to go about this and why does the

Initializing reference member variable with literal

走远了吗. 提交于 2020-01-24 08:41:25
问题 In the following code , I am initializing a reference variable with a literal. class ABC { public: const int& a; ABC():a(43) { } void newfoo() { printf("NEWFOO %d",a); } }; int main() { ABC obj; obj.newfoo(); } The output of this program is NEWFOO 32767 which seems illogical when I know that the following code works just fine. int main() { const int& b=3; printf("%d",b); } What is happening here ? If compiler declares some temp variable during initializing of the reference variable , then isn

Why GCC does not report uninitialized variable?

亡梦爱人 提交于 2020-01-24 03:23:26
问题 #include <ios> #include <iostream> #include <map> using namespace std; int main() { ios_base::sync_with_stdio(false); map<int, int> v; int i; int t; while (cin >> i) { v[i] = t++; } auto mi = i; auto mt = t; for (const auto p : v) { if (p.second < mt) { mi = p.first; mt = p.second; } } cout << mi << '\n'; return 0; } The abovementioned program makes heavy use of an uninitialized variable t , but GCC does not report it with -Wall or -Wuninitialized. Why is it so? It is worth noting that Clang

WinForms Control - Action after it is fully initialized

我是研究僧i 提交于 2020-01-24 01:02:08
问题 To a WinForms control, I would like to add a handler after the container has initialized the control (or even better, after the parent has initialized all contained controls). Reason: The custom control has an option to trigger an action automatically. It should also trigger when this option is first enabled. However, at this point, other properties (like event handlers) are not yet wired up correctly, and thus the effect is not as expected. Is this possible? [edit] I understand that this

How do I value-initialize a Type* pointer using Type()-like syntax?

早过忘川 提交于 2020-01-23 04:31:08
问题 Variables of built-in types can be value-initialized like this: int var = int(); this way I get the default value of int without hardcoding the zero in my code. However if I try to do similar stuff for a pointer: int* ptr = int*(); the compiler (Visual C++ 10) refuses to compile that (says type int unexpected ). How do I value-initialize a pointer in similar manner? 回答1: How do I value-initialize a Type* pointer using Type()-like syntax? You cannot. The syntax T() is defined in 5.2.3/1,2 (C+

Default argument using curly braces initializer

谁说胖子不能爱 提交于 2020-01-22 20:37:49
问题 I have this snippet of code which seems to work well: class foo{/* some member variables and functions*/}; void do_somthing(foo x={}){} int main(){ do_somthing(); } I used to use void do_somthing(foo x=foo()){} to default the x argument but I see this way ={} in some book or online example(can not remember). Is it totally ok to use it? Is there any difference between the two methods? 回答1: foo x=foo() is copy initialization, Initializes an object from another object and foo() is value

Default argument using curly braces initializer

安稳与你 提交于 2020-01-22 20:37:06
问题 I have this snippet of code which seems to work well: class foo{/* some member variables and functions*/}; void do_somthing(foo x={}){} int main(){ do_somthing(); } I used to use void do_somthing(foo x=foo()){} to default the x argument but I see this way ={} in some book or online example(can not remember). Is it totally ok to use it? Is there any difference between the two methods? 回答1: foo x=foo() is copy initialization, Initializes an object from another object and foo() is value

Use of variable in own initializer

余生长醉 提交于 2020-01-22 17:07:05
问题 [basic.scope.pdecl]/1 of the C++20 standard draft had the following (non-normative) example in a note (partial quote from before the merge of pull request 3580, see answer to this question): unsigned char x = x; [...] x is initialized with its own (indeterminate) value. Does this actually have well-defined behavior in C++20? Generally the self-initialization of the form T x = x; has undefined behavior by virtue of x 's value being indeterminate before initialization is completed. Evaluating

Explicit initialization of struct/class members

谁说胖子不能爱 提交于 2020-01-22 13:35:22
问题 struct some_struct{ int a; }; some_struct n = {}; n.a will be 0 after this; I know this braces form of initialization is inherited from C and is supported for compatibility with C programs, but this only compiles with C++, not with the C compiler. I'm using Visual C++ 2005. In C this type of initialization struct some_struct n = {0}; is correct and will zero-initialize all members of a structure. Is the empty pair of braces form of initialization standard? I first saw this form of

Explicit initialization of struct/class members

匆匆过客 提交于 2020-01-22 13:35:11
问题 struct some_struct{ int a; }; some_struct n = {}; n.a will be 0 after this; I know this braces form of initialization is inherited from C and is supported for compatibility with C programs, but this only compiles with C++, not with the C compiler. I'm using Visual C++ 2005. In C this type of initialization struct some_struct n = {0}; is correct and will zero-initialize all members of a structure. Is the empty pair of braces form of initialization standard? I first saw this form of