Include swift class as an Instance variable in a C++ class

Is there a way to achieve the following using C++/Swift interoperability:

class MyCppClass
{
public:
...
...

private:
   bool member1;
   ACppClass member2;
   ASwiftClass member3;
}

I'm aware of the recent C++/Objective-C interoperability compiler setting, but can't find any information on whether this is possible.

I've watched the Apple video: https://developer.apple.com/videos/play/wwdc2023/10172/

and seen this post from Quinn: https://developer.apple.com/forums/thread/768928

but I don't see anyone discussing this kind of situation. Thanks in advance.

Yes, this basically works.

One oddity is that the C++ wrapper doesn’t seem to provide a default constructor, so you have to use a factory method.

If you can’t make it work, post a concrete example and I’ll compare with what I do.

Seems like my problem is that I can't get the following to work: include/import the auto-generated Swift header in either my C++ header or my C++ source file (which is a *.mm file), but I CAN include it in Objective-C++ source (also *.mm) and header files. When in the latter files I can CMD click on the auto-generated header to open it.

Anyone got any suggestions?

FWIW In my project settings I have set the name of the auto-generated Swift header file to be consistent across all of the build types, so it's not a debug build file naming issue.

Seems like my problem is that I can't get the following to work: include/import the auto-generated Swift header in either my C++ header or my C++ source file (which is a *.mm file)

What exactly happens when you try?

Thanks for the help endecotp. I found the issue: my source file was included in multiple target dependencies and one of them wasn't using the Swift compiler. Found it by looking at the build transcript. I'll see if I can get the member variable thing working now, and will post a solution.

As is often the case, the problem wasn't where I thought it was :)

Include swift class as an Instance variable in a C++ class
 
 
Q