Your constructor:
CModel(CMFCApplicationSet ApplicationSet);
takes object of type CMFCApplicationSet
by value, meaning that the copy of passed object is created and this copy is then used within its body. When you do the following:
pModelSet = &ApplicationSet;
you are actually storing the addres of temporary object, which is destructed when execution goes out of the scope of the constructor. If you try to dereference this pointer afterwards, it will produce undefined behavior.
Some might suggest you to pass a pointer, but if you look at your CModel
class closely, you will see that it has only one constructor and it takes the PessoaSet
object, i.e. the instance of CModel
requires the existance of some object of type PessoaSet
, i.e. you should keep a reference, not a pointer:
class CModel
{
public:
CModel(CMFCApplicationSet& ApplicationSet) : modelSet(ApplicationSet) { }
CMFCApplicationSet& modelSet;
}
and in the other class that contains instance of CModel
:
class CMFCApplicationDoc
{
public:
CMFCApplicationDoc() :
Model(m_MFCApplicationSet) { }
CMFCApplicationSet m_MFCApplicationSet;
CModel Model;
}
just note that the order, in which members of CMFCApplicationDoc
are declared actually matters here, since you want m_MFCApplicationSet
to be initialized earlier than Model
and the standards (12.6.2 §5) says: "nonstatic data members shall be initialized in the order they were declared in the class definition".