Skip to content

Commit b8a4ffc

Browse files
committed
[cxx-interop] Add documentation about calling ctor or static factory of SWIFT_SHARED_REFERENCE types as Swift Initializer
1 parent 838b4f1 commit b8a4ffc

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

documentation/cxx-interop/index.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,8 +1239,8 @@ To specify that a C++ type is a shared reference type, use the `SWIFT_SHARED_REF
12391239
class SharedObject : IntrusiveReferenceCounted<SharedObject> {
12401240
public:
12411241
SharedObject(const SharedObject &) = delete; // non-copyable
1242+
SharedObject(); // Constructor
12421243

1243-
static SharedObject* create();
12441244
void doSomething();
12451245
} SWIFT_SHARED_REFERENCE(retainSharedObject, releaseSharedObject);
12461246

@@ -1250,11 +1250,19 @@ void releaseSharedObject(SharedObject *);
12501250
12511251
Now that `SharedObject` is imported as a reference type in Swift, the programmer will be able to use it in the following manner:
12521252
```swift
1253-
let object = SharedObject.create()
1253+
// Call the C++ constructor of SharedObject using Swift initializer syntax.
1254+
let object = SharedObject()
12541255
object.doSomething()
12551256
// `object` will be released here.
12561257
```
12571258

1259+
You can create instances of `SharedObject` directly in Swift by calling its C++ constructor through a Swift initializer.
1260+
1261+
Alternatively, you can construct instances using a user-defined static factory function, provided that the factory function is annotated with `SWIFT_NAME("init(...)")`, where the number of `_` placeholders matches the number of parameters in the factory function
1262+
1263+
> **Note**: If a C++ constructor and a user-annotated static factory (via SWIFT_NAME) have identical parameter signatures, Swift prefers the static factory when resolving initializer calls.
1264+
1265+
12581266
### Inheritance and Virtual Member Functions
12591267

12601268
Similar to value types, casting an instance of a derived reference type to a

0 commit comments

Comments
 (0)