You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/posts/cpp-for-cscharp.md
+26-23Lines changed: 26 additions & 23 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -136,45 +136,48 @@ This table describes the most commonly used headers in the standard library:
136
136
137
137
## Initialization
138
138
139
+
**Stack / Heap:**
140
+
139
141
C++ allows you to initialize objects on the stack or the heap. But In C# you don't get to choose, classes will be on the heap because they are reference types and structs on the stack because they are value types, and in C# you must always use the ``new`` keyword.
140
142
141
-
Initializing on the stack in C++ syntax:
142
-
```cpp
143
-
// cpp
143
+
**Copy / Direct:**
144
144
145
-
// with parentheses
146
-
Type test(args);
147
-
Type test = Type(args);
145
+
Another thing to keep in mind about C++ initialization in particular is that there is a distinction between copy and direct initialization, direct initialization is slightly faster because there is no need for an unnecessary copy operation to be done.
148
146
149
-
// with curly braces
150
-
Type test{args};
151
-
Type test = Type{args};
152
-
Type test = {args};
147
+
**C++ stack initialization:**
148
+
```cpp
149
+
// direct
150
+
Type test(x); // parentices
151
+
Type test{x}; // curly braces
152
+
153
+
// copy
154
+
Type test = Type(x); // parentices
155
+
Type test = Type{x}; // curly braces
156
+
157
+
// copy (shorthand)
158
+
Type test = x; // implicit conversion
159
+
Type test = (x); // same as the above because () does nothing
160
+
Type test = {x}; // prevents narrowing conversions
153
161
```
154
162
155
-
Initializing on the heap in C++ syntax:
163
+
**C++ heap initialization:**
156
164
```cpp
157
-
// cpp
158
-
Type* test = new Type(args); // with parentheses
159
-
Type* test = new Type{args}; // with curly braces
165
+
Type* test = new Type(x); // parentheses
166
+
Type* test = new Type{x}; // curly braces
160
167
```
161
168
162
-
Initializing on the heap in C# syntax (only way):
169
+
**C# initialization:**
163
170
```csharp
164
-
// csharp
165
-
Typetest=newType(args); // with parentheses
171
+
Typetest=newType(x); // the only way in csharp
166
172
```
167
173
168
-
here is a crude comparison table of how the C++ ways translate to C# syntax:
169
-
170
-

171
-
172
-
Things to remember about initialization:
174
+
**Things to remember about initialization:**
173
175
174
176
- C++ gives more control over stack vs heap
175
177
- C++ supports multiple syntaxes: parentheses and curly braces
178
+
- C++ gives more control about when copy operations are used
176
179
- C# classes are reference types (heap), structs are value types (stack)
177
-
- C# always requires new for both structs and classes
180
+
- C# always requires the new keyword for both structs and classes
0 commit comments