Hi all,
Basically I need to create a managed wrapper for the class "A" below:
class A : public CModule
{
public:
A(B& bees);
virtual ~A();
....etc. (it's actually a very big class)
}
As you can see, the constructor above takes an input which is a
reference to another unmanaged class called "B"
class B
{
public:
B();
virtual ~B();
....etc. (also a big class)
}
I have successfully created a wrapper for 'B'. However, the
constructor for 'A' has me stymied. How do you create a managed
wrapper for an unmanaged class that requires an instance of another
unmanaged class in its constructor? Any help would be appreciated.
Marcus Heege - 27 Apr 2006 17:00 GMT
> Hi all,
>
[quoted text clipped - 28 lines]
> wrapper for an unmanaged class that requires an instance of another
> unmanaged class in its constructor? Any help would be appreciated.
Why don't you create a constructor that takes a B^ argument?
bidalah@yahoo.com - 27 Apr 2006 19:00 GMT
Hi Marcus,
That's my problem. I can't find a way of coding the constructor that
doesn't error out. If I use the wrapper for B as the argument (m_B),
the error is "Cannot convert (managed value) to (unamanaged value). If
I try to use B directly the error is "Cannot instantiate nonstatic
members in a __gc class. I've tried all kinds of different
constuctors.
How do you create a constructor for a wrapper class when the unmanaged
class uses another unmanaged class as input?
Marcus Heege - 27 Apr 2006 19:21 GMT
> Hi Marcus,
>
[quoted text clipped - 7 lines]
> How do you create a constructor for a wrapper class when the unmanaged
> class uses another unmanaged class as input?
Is that what you want?
#using <mscorlib.dll>
class A
{
};
class B
{
public:
B(A& a) {}
};
public __gc class WA
{
public private:
A* m_pA;
public:
WA() : m_pA(new A) {}
~WA() { if (m_pA) delete m_pA; }
};
public __gc class WB
{
B* m_pB;
public:
WB(WA __gc* pWA) : m_pB(new B(*pWA->m_pA)) {}
~WB() { if (m_pB) delete m_pB; }
};
Tamas Demjen - 27 Apr 2006 19:20 GMT
> I have successfully created a wrapper for 'B'. However, the
> constructor for 'A' has me stymied. How do you create a managed
> wrapper for an unmanaged class that requires an instance of another
> unmanaged class in its constructor? Any help would be appreciated.
Try something like this:
class B
{
};
class A
{
public:
A() { }
A(const B&) { }
};
ref class MB
{
public:
MB() : b(new B) { }
B& GetUnmanaged() { return *b; }
private:
B* b;
};
ref class MA
{
public:
MA() : a(new A) { }
MA(MB^ src) : a(new A(src->GetUnmanaged())) { }
private:
A* a;
};
Tom