Skip to content

Commit efc6200

Browse files
committed
improved init chapter
1 parent 776e0c8 commit efc6200

2 files changed

Lines changed: 26 additions & 23 deletions

File tree

content/posts/cpp-for-cscharp.md

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -136,45 +136,48 @@ This table describes the most commonly used headers in the standard library:
136136

137137
## Initialization
138138

139+
**Stack / Heap:**
140+
139141
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.
140142

141-
Initializing on the stack in C++ syntax:
142-
```cpp
143-
// cpp
143+
**Copy / Direct:**
144144

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.
148146

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
153161
```
154162
155-
Initializing on the heap in C++ syntax:
163+
**C++ heap initialization:**
156164
```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
160167
```
161168

162-
Initializing on the heap in C# syntax (only way):
169+
**C# initialization:**
163170
```csharp
164-
// csharp
165-
Type test = new Type(args); // with parentheses
171+
Type test = new Type(x); // the only way in csharp
166172
```
167173

168-
here is a crude comparison table of how the C++ ways translate to C# syntax:
169-
170-
![init](/images/table-init.png)
171-
172-
Things to remember about initialization:
174+
**Things to remember about initialization:**
173175

174176
- C++ gives more control over stack vs heap
175177
- C++ supports multiple syntaxes: parentheses and curly braces
178+
- C++ gives more control about when copy operations are used
176179
- 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
178181

179182
## Public / Private
180183

static/images/table-init.png

-31.8 KB
Binary file not shown.

0 commit comments

Comments
 (0)