2011-09-15 115 views
1

我想用C++/cli来包装非托管的C++代码。在C++/cli中,一个类必须被声明为抽象如果它具有所有纯虚拟函数。现在假设我有下面的代码在C++:指向抽象类的指针

class A{ 
public: 
    virtual ~A(); 
    virtual void foo() = 0; 
    virtual void boo() = 0; 
    virtual void goo() = 0; 
}; 
inline 
A::~A() 
{ 
} 

class B 
{ 
public: 
    B(); 
    A* getA() const; 
}; 

inline A* B::getA() const 
{ 
    //do something 
    return (A *) //of something; 
} 

按照上面我可以返回A *没有任何错误。现在假设我包上面的代码如下:

public ref class manA abstract 
{ 
public: 
    manA(A*); 
    virtual ~manA(); 
    virtual void Foo() = 0; 
    virtual void Boo() = 0; 
    virtual void Goo() = 0; 
private: 
    A* m_A; 
}; 

inline 
manA::~manA() 
{ 
} 

inline 
manA::manA(A*) 
{ 
    //data marshalling to convert A* to manA 
} 

public ref class manB 
{ 
public: 
    manB(); 
    manA^GetA(); 
private: 
    B * m_B; 
}; 

inline manB::manB() 
{ 
    m_B = new B; 
} 

inline manA^manB::GetA() 
{ 
    A *value = m_B->getA(); 
    return gcnew manA(value); 
} 

现在,如果我做了以上我得到一个声明为“抽象”类不能被实例化错误。

有没有解决方案?

注:类A定义了它的所有可能实现的接口。所以也许有一种方法可以定义manA,使其不是抽象的,因此可以被实例化?

我找到了一个解决问题的办法:

取出构造的manA和使用属性

public: 
property A* UnmanagedObject 
{ 
    void set(A* unmanagedObjPtr) 
    { 
    //Or other data marshalling 
    m_A = (A *)(unmanagedObjPtr); 
    } 
} 

和内部MANB做:

inline manA^manB::GetA() 
{ 
    A *value = m_B->getA(); 
    manA^final; 
    final->UnmanagedObject = value; 
    return final; 
} 
+0

您需要在Derived类中实现Abstract类的所有纯虚函数,否则您的派生类也将变为Abstract类。 –

+2

我在代码中看不到任何继承。 –

+0

B不是来自A – Saurabh

回答

0

inline manB::manB() { m_B = new B; }

我认为在这一行发生了错误,因为B是从抽象类A派生的,并没有实现在A类中定义的纯函数。所以B也是一个抽象类。你永远不能建立抽象类B的一个实例

+0

B不是来自A – Saurabh

0

我认为这个问题是这样的

return gcnew manA(value) 

你实例化的manA你声明为一个抽象类...

+0

我知道,但如果我不这样做,那么如何包装_getA()_函数? – Saurabh

1

编写封装器不意味着写同一个本地课程。不要让manA成为抽象类,你的问题已经消失。

public ref class manA abstract 
{ 
public: 
    manA(A*); 
    virtual ~manA(); 
    virtual Foo() { m_A->foo(); } 
    virtual Boo() { m_A->boo(); } 
    virtual Goo() { m_A->goo(); } 
//Also there should be an error like "Hey, What is the return type of those functions?" 
//"virtual void Foo()" or "virtual int Foo()" or something else 
private: 
    A* m_A; 
}; 
+0

对不起。只是把它作为我实际上在做什么的一个例子 – Saurabh

+0

@Saurabh:给你一个问题。在真正的代码中,你有从'manA'继承的类吗,必须'manA'实际上是抽象的吗? –

+0

我有......所以它一定是抽象的。我想我已经得到了解决方案。我编辑了我的问题,回答 – Saurabh

0

利用仿制药。

public ref class manB 
{ 
public: 
    manB() 
    { 
    m_B = new B(); 
    } 

    generic<T> where T : manA 
    manA^GetA() 
    { 
    A *value = m_B->getA(); 
    T^final = Activator::CreateInstance<T>(); 
    final->UnmanagedObject = value; 
    return final; 
    } 
    /* or 
    generic<T> where T : manA 
    T^GetA() 
    { 
    A *value = m_B->getA(); 
    T^final = Activator::CreateInstance<T>(); //this requires T has a public default contructor. 
    final->UnmanagedObject = value; 
    return final; 
    } 
    */ 

    !manB() 
    { 
    delete m_B; //don't forget to release the native resource 
    } 
private: 
    B * m_B; 
};