views:

258

answers:

2

I am writing a .NET assembly using C++/CLI (version 9.0), and I would like to use the PIMPL idiom to avoid putting unnecessary stuff in my public header. Unfortunately, when I try to forward declare a class, and then use a tracking handle to it, I get Linker warning 4248:

warning LNK4248: unresolved typeref token (0100000E) for 'MyNamespace.PrivateClass'; image may not run

This seems to be the case whether I use a CLI class or a native class for the implementation class.

Example code appears below:

namespace MyNamespace
{
    ref class PrivateClass; // forward dec

    ref class MyPublicClass
    {
    private:
     PrivateClass^ m_Imp;
    };
}

The Microsoft explanation for the warning is not too informative, unfortunately.

+1  A: 

Upon further digging and reflecting I have discovered that in some respects .NET does not really need to support PIMPL in the same way as C++, since you can mark a class private to an assembly - this essentially has the same effect, in some ways. However often the PIMPL idiom is used to hide headers that you don't want the client to have to compile. But of course .NET assemblies are not "included" like the headers for C++ are - so I guess there really isn't an issue there either.

Brian Stewart
+2  A: 

I think you're using two technologies which do not really sit together well:

The natural application for pimpl is to avoid having to make header file changes all the time thereby causing big recompilations on big C++ projects.

The natural application for C++/cli is to write skinny little interop pieces, and the default behaviour of VS on these projects is to put all the code into the headers, which is about as anti-pimpl as you can get.

If you're writing something large enough to warrant pimpl, I wouldn't recommend C++/cli. If you're writing something small enough to make C++/cli appropriate, I wouldn't bother with pimpl.

YMMV of course, but that would be my take on it...

Will Dean