From Jason Turner

[class.mfct.non-static]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmphxk3lxqb/{from.md → to.md} +0 -157
tmp/tmphxk3lxqb/{from.md → to.md} RENAMED
@@ -1,157 +0,0 @@
1
- ### Non-static member functions <a id="class.mfct.non-static">[[class.mfct.non-static]]</a>
2
-
3
- A non-static member function may be called for an object of its class
4
- type, or for an object of a class derived [[class.derived]] from its
5
- class type, using the class member access syntax ([[expr.ref]],
6
- [[over.match.call]]). A non-static member function may also be called
7
- directly using the function call syntax ([[expr.call]],
8
- [[over.match.call]]) from within its class or a class derived from its
9
- class, or a member thereof, as described below.
10
-
11
- If a non-static member function of a class `X` is called for an object
12
- that is not of type `X`, or of a type derived from `X`, the behavior is
13
- undefined.
14
-
15
- When an *id-expression* [[expr.prim.id]] that is not part of a class
16
- member access syntax [[expr.ref]] and not used to form a pointer to
17
- member [[expr.unary.op]] is used in a member of class `X` in a context
18
- where `this` can be used [[expr.prim.this]], if name lookup
19
- [[basic.lookup]] resolves the name in the *id-expression* to a
20
- non-static non-type member of some class `C`, and if either the
21
- *id-expression* is potentially evaluated or `C` is `X` or a base class
22
- of `X`, the *id-expression* is transformed into a class member access
23
- expression [[expr.ref]] using `(*this)` [[class.this]] as the
24
- *postfix-expression* to the left of the `.` operator.
25
-
26
- [*Note 1*: If `C` is not `X` or a base class of `X`, the class member
27
- access expression is ill-formed. — *end note*]
28
-
29
- This transformation does not apply in the template definition context
30
- [[temp.dep.type]].
31
-
32
- [*Example 1*:
33
-
34
- ``` cpp
35
- struct tnode {
36
- char tword[20];
37
- int count;
38
- tnode* left;
39
- tnode* right;
40
- void set(const char*, tnode* l, tnode* r);
41
- };
42
-
43
- void tnode::set(const char* w, tnode* l, tnode* r) {
44
- count = strlen(w)+1;
45
- if (sizeof(tword)<=count)
46
- perror("tnode string too long");
47
- strcpy(tword,w);
48
- left = l;
49
- right = r;
50
- }
51
-
52
- void f(tnode n1, tnode n2) {
53
- n1.set("abc",&n2,0);
54
- n2.set("def",0,0);
55
- }
56
- ```
57
-
58
- In the body of the member function `tnode::set`, the member names
59
- `tword`, `count`, `left`, and `right` refer to members of the object for
60
- which the function is called. Thus, in the call `n1.set("abc",&n2,0)`,
61
- `tword` refers to `n1.tword`, and in the call `n2.set("def",0,0)`, it
62
- refers to `n2.tword`. The functions `strlen`, `perror`, and `strcpy` are
63
- not members of the class `tnode` and should be declared elsewhere.[^2]
64
-
65
- — *end example*]
66
-
67
- A non-static member function may be declared `const`, `volatile`, or
68
- `const` `volatile`. These *cv-qualifier*s affect the type of the `this`
69
- pointer [[class.this]]. They also affect the function type [[dcl.fct]]
70
- of the member function; a member function declared `const` is a *const
71
- member function*, a member function declared `volatile` is a *volatile
72
- member function* and a member function declared `const` `volatile` is a
73
- *const volatile member function*.
74
-
75
- [*Example 2*:
76
-
77
- ``` cpp
78
- struct X {
79
- void g() const;
80
- void h() const volatile;
81
- };
82
- ```
83
-
84
- `X::g` is a const member function and `X::h` is a const volatile member
85
- function.
86
-
87
- — *end example*]
88
-
89
- A non-static member function may be declared with a *ref-qualifier*
90
- [[dcl.fct]]; see  [[over.match.funcs]].
91
-
92
- A non-static member function may be declared virtual [[class.virtual]]
93
- or pure virtual [[class.abstract]].
94
-
95
- #### The `this` pointer <a id="class.this">[[class.this]]</a>
96
-
97
- In the body of a non-static [[class.mfct]] member function, the keyword
98
- `this` is a prvalue whose value is a pointer to the object for which the
99
- function is called. The type of `this` in a member function whose type
100
- has a *cv-qualifier-seq* cv and whose class is `X` is “pointer to cv
101
- `X`”.
102
-
103
- [*Note 1*: Thus in a const member function, the object for which the
104
- function is called is accessed through a const access
105
- path. — *end note*]
106
-
107
- [*Example 1*:
108
-
109
- ``` cpp
110
- struct s {
111
- int a;
112
- int f() const;
113
- int g() { return a++; }
114
- int h() const { return a++; } // error
115
- };
116
-
117
- int s::f() const { return a; }
118
- ```
119
-
120
- The `a++` in the body of `s::h` is ill-formed because it tries to modify
121
- (a part of) the object for which `s::h()` is called. This is not allowed
122
- in a const member function because `this` is a pointer to `const`; that
123
- is, `*this` has `const` type.
124
-
125
- — *end example*]
126
-
127
- [*Note 2*: Similarly, `volatile` semantics [[dcl.type.cv]] apply in
128
- volatile member functions when accessing the object and its non-static
129
- data members. — *end note*]
130
-
131
- A member function whose type has a *cv-qualifier-seq* *cv1* can be
132
- called on an object expression [[expr.ref]] of type *cv2* `T` only if
133
- *cv1* is the same as or more cv-qualified than *cv2*
134
- [[basic.type.qualifier]].
135
-
136
- [*Example 2*:
137
-
138
- ``` cpp
139
- void k(s& x, const s& y) {
140
- x.f();
141
- x.g();
142
- y.f();
143
- y.g(); // error
144
- }
145
- ```
146
-
147
- The call `y.g()` is ill-formed because `y` is `const` and `s::g()` is a
148
- non-const member function, that is, `s::g()` is less-qualified than the
149
- object expression `y`.
150
-
151
- — *end example*]
152
-
153
- [*Note 3*: Constructors and destructors cannot be declared `const`,
154
- `volatile`, or `const` `volatile`. However, these functions can be
155
- invoked to create and destroy objects with cv-qualified types; see 
156
- [[class.ctor]] and  [[class.dtor]]. — *end note*]
157
-