C++ MFC - Application crashes upon attribute definition

79 Views Asked by At

I have a C++ class, ClassA. I want to, inside ClassA, define ClassB and then define an attribute of ClassA of type ClassB.

I have another class, ClassC which holds a pointer variable of the type ClassA. If I attempt to point this pointer to an instance of ClassA, the application crashes. However if I remove the attribute m_classB, everything works fine. So something about the instance of ClassC attempting to point to the ClassA instance that contains a ClassB is causing it to crash, but a ClassA that does NOT contain a ClassB is fine... if that makes sense.

My code is below:

class __declspec(dllexport) ClassA
{
public:
    class ClassB {
    public:
        int m_nID;
        CString m_string;

        ClassB() :
            m_nID(-1),
            m_string(_T(""))
        {

        }
    };

    ClassB m_classB; // If i comment this out, then the application loads no problem
};

What is quite strange is that if I replace ClassB m_classB; with vector<ClassB> m_vclassB;, it all works fine.

If I use ClassB* m_classB instead then the same crash occurs.

Cheers!

0

There are 0 best solutions below