Is it possible to instantiate a class inside a member function of this class? For example, let's say I have a class CMyClasswith a member function CMyClass::MemberFunc, and I want to create another instance CMyClassinside CMyClass::MemberFunc.
void CMyClass::MemberFunc( void )
{
CMyClass * pMyClass = new CMyClass();
}
Is it legal / safe? I know that it compiles. I am worried about recursion. Will I encounter a recursion error when I first create CMyClassfrom the main application?
void main( void )
{
static CMyClass * s_pMyClass = new CMyClass();
}
Or will recursion occur only when a particular member function is called with an additional instance of the class?
void CMyClass::MemberFunc( void )
{
CMyClass * pMyClass = new CMyClass();
pMyClass->MemberFunc();
}
In other words, can I safely create an instance of a given class inside a member function of this class if I don't call this member function of a second instance of this class? Thank.