Forward declaration of C# types in managed C++ headers

  • Thread starter Thread starter Eckart Haug
  • Start date Start date
E

Eckart Haug

I'm working with C# objects from managed C++ using the gcroot template.


There'a a C++ header containing the definition of a C++ class:

#using <mscorlib.dll>
#include <vcclr.h>
#using <CSharpModule.dll>

class CPlusPlusClass
{
....

gcroot<TLNameSpace::SubNameSpace::SomeType*> _csharpInstance;

....
}


and a C++ source file containing the class definition


#include "CPlusPlusClass.h"
using namespace TLNameSpace::SubNameSpace;


void CPlusPlusClass::createCSharp()
{
gcroot = new SomeType();
}

void CPlusPlusClass::doSomething()
{
gcroot->someCSharpMethod();
}

.....


In this example, I need the #using <CSharpModule.dll> directive in the C++ header. One way to avoid this is
to split CPlusPlusClass into CPlusPlusInterface and CPlusPlus Implementation.
A more elegant technique in C++ would be a forward declaration for TLNameSpace::SubNameSpace::SomeType in
the C++ header. However, I didn't find out how to do this.
The most promising try was

namespace TLNameSpace {
namespace SubNameSpace {
__gcc class SomeType;
}
}

At least the header compiled. On the #using <CSharpModule.dll> line, I got an 'already defined' error.
Obviously C++ and C# namespaces are just a little bit the same ....

My question is:

- Is there a way to do the forward declaration described above ?


I'd appreciate any hint/solution for this
 
You can add your C# assembly in a project's references thus you may not
import it explicitly.
 
Thanx for the quick reply. Your suggestion would mean that I'd have to add a C# reference to any C++ module
that includes the C++ header. There's lots of them and I'd (for the moment) like to have the C++ <-> C#
interface encapsulated in one C++ module.
 
Thanx for the quick reply. Your suggestion would mean that I'd have to add a C# reference to any C++ module
that includes the C++ header. There's lots of them and I'd (for the moment) like to have the C++ <-> C#
interface encapsulated in one C++ module.
 
Back
Top