member-access

Does the C# compiler get the Color Color rule wrong with const type members?

谁都会走 提交于 2020-07-16 17:41:14
问题 Okay, so the C# Language Specification has a special section (old version linked) on the Color Color rule where a member and its type has the same name. Well-known guru Eric Lippert once blogged about it. The question I am going to ask here is in a sense (not) quite the same that was asked in the thread Circular definition in a constant enum. You can go and upvote that other question if you like. Now for my question. Consider this code: namespace N { public enum Color { Green, Brown, Purple,

Why is an object not stongly typed in a foreach with var?

徘徊边缘 提交于 2020-01-24 11:06:37
问题 i was writing the following if(this.tabControl1.TabPages.Count != ImagesList.Count()) { foreach (var item in this.tabControl1.TabPages) { } } and i couldn't access the controls inside each item using item. But with a defining it's type like if(this.tabControl1.TabPages.Count != ImagesList.Count()) { foreach (TabPage item in this.tabControl1.TabPages) { } } i could easily access them using item.Controls so i was wondering why do i really need to define/cast those items as TabPage , shouldn't

Easier way to write encapsulated parent/child data structure?

拜拜、爱过 提交于 2019-12-25 09:03:36
问题 From time to time I find myself often writing a data structure of "parents" and "children", where: A parent has references to 0 to N distinct children. A child has a reference to 0 parents or 1 parent. The reference must be mutual. For any given parent, any child that it references must also reference the given parent back. For any given child, the parent that it references must reference the given child back. It's impossible to violate the above rules through use of members accessible from

Is taking the address of a member of an uninitialized object well defined?

心已入冬 提交于 2019-12-19 08:14:07
问题 Consider the following example. When bar is constructed, it gives it's base type ( foo ) constructor the address of my_member.y where my_member is data member that hasn't been initialized yet. struct foo { foo(int * p_x) : x(p_x) {} int * x; }; struct member { member(int p_y) : y(p_y) {} int y; }; struct bar : foo { bar() : foo(&my_member.y), my_member(42) {} member my_member; }; #include <iostream> int main() { bar my_bar; std::cout << *my_bar.x; } Is this well defined? Is it legal to take

Is taking the address of a member of an uninitialized object well defined?

南笙酒味 提交于 2019-12-19 08:13:04
问题 Consider the following example. When bar is constructed, it gives it's base type ( foo ) constructor the address of my_member.y where my_member is data member that hasn't been initialized yet. struct foo { foo(int * p_x) : x(p_x) {} int * x; }; struct member { member(int p_y) : y(p_y) {} int y; }; struct bar : foo { bar() : foo(&my_member.y), my_member(42) {} member my_member; }; #include <iostream> int main() { bar my_bar; std::cout << *my_bar.x; } Is this well defined? Is it legal to take

What exactly is the meaning of the footnote mentioned in [expr.ref]/1?

末鹿安然 提交于 2019-12-10 03:16:55
问题 [expr.ref]/1: A postfix expression followed by a dot . or an arrow -> , optionally followed by the keyword template (17.2), and then followed by an id-expression , is a postfix expression. The postfix expression before the dot or arrow is evaluated; 67 the result of that evaluation, together with the id-expression , determines the result of the entire postfix expression. 67) If the class member access expression is evaluated, the subexpression evaluation happens even if the result is

What exactly is the meaning of the footnote mentioned in [expr.ref]/1?

好久不见. 提交于 2019-12-05 03:08:54
[expr.ref]/1 : A postfix expression followed by a dot . or an arrow -> , optionally followed by the keyword template (17.2), and then followed by an id-expression , is a postfix expression. The postfix expression before the dot or arrow is evaluated; 67 the result of that evaluation, together with the id-expression , determines the result of the entire postfix expression. 67) If the class member access expression is evaluated, the subexpression evaluation happens even if the result is unnecessary to determine the value of the entire postfix expression, for example if the id-expression denotes

In C++11, protected means public?

二次信任 提交于 2019-12-03 09:11:28
问题 Continuing something learned in C++ error: base function is protected ... The C++11 pointer-to-member rules effectively strip the protected keyword of any value, because protected members can be accessed in unrelated classes without any evil/unsafe casts. To wit: class Encapsulator { protected: int i; public: Encapsulator(int v) : i(v) {} }; Encapsulator f(int x) { return x + 2; } #include <iostream> int main(void) { Encapsulator e = f(7); // forbidden: std::cout << e.i << std::endl; because

In C++11, protected means public?

倖福魔咒の 提交于 2019-12-02 23:20:23
Continuing something learned in C++ error: base function is protected ... The C++11 pointer-to-member rules effectively strip the protected keyword of any value, because protected members can be accessed in unrelated classes without any evil/unsafe casts. To wit: class Encapsulator { protected: int i; public: Encapsulator(int v) : i(v) {} }; Encapsulator f(int x) { return x + 2; } #include <iostream> int main(void) { Encapsulator e = f(7); // forbidden: std::cout << e.i << std::endl; because i is protected // forbidden: int Encapsulator::*pi = &Encapsulator::i; because i is protected //

Will the compiler-generated default constructor be public?

为君一笑 提交于 2019-12-01 04:26:23
When I write a class Widget.java public class Widget { int data; String name; } will the compiler-generated constructor be public or default ? public would be like public class Widget { int data; String name; public Widget() {} } whereas default similar to public class Widget { int data; String name; Widget() {} } It depends on your class visibility .The compiler uses the class visibility and generates a no-arg default constructor with the same visibility As said in JLS If a class contains no constructor declarations, then a default constructor that takes no parameters is automatically