c++ pointer class
if i have class CAnalyzer, and i want to make a pointer from this class to new class name CManager. how c开发者_运维知识库an i do it? note: i need to make the pointer inside init func in CAnalyzer. thanks
class CManager; // Forward declaration (may not be needed)
class CAnalyzer
{
// Other stuff goes here
private:
CManager *p_manager;
};
I don't understand what you mean by "I need to make the pointer inside init func in CAnalyzer".
By init func, did you mean the constructor?
CAnalyzer::CAnalyzer()
{
CManager *pManager = new CManager();
}
I'm not sure I understand exactly what you're asking, so I will guess.
You want to write a member function of CAnalyzer
that returns a pointer to a new instance of a CManager
? You can do that like this:
CManager* CAnalyzer::CreateManager()
{
return new CManager;
}
You should, however, use smart pointers rather than raw pointers in the interest of robust programming.
精彩评论