Link to home
Start Free TrialLog in
Avatar of Belthazor
Belthazor

asked on

Create a pointer to an existing Object....

How can I create a pointer to an existing Object?
I've got a class (CMyListCtrl) derived from CListCtrl. I want to call a non-static function of CMyProjectDialog, wich is the Dialog Class of my Project, out of CMyListCtrl. There's an Object of CMyListCtrl in CMyProjectDialog.

Thanks for help.

Belthazor
Avatar of oferh
oferh

you dont need a pointer to the parent class. if you want to make sure that the parent method is called then explicitly call it:
CListCtrl::func();
Nothing more to say ...
Avatar of Belthazor

ASKER

I dont want to call a Function of CListCtrl. I want to call a non-static function of CMyProjectDialog. In this class I've got an Object of CMyListCtrl. Now I want to call a function of CMyProjectDialog out of CMyListCtrl.
I hope this is a better description of my Problem.
ASKER CERTIFIED SOLUTION
Avatar of brazulewicz
brazulewicz

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Axter
Does CMyListCtrl have a pointer to CMyProjectDialog?

If not, you need to add a member variable to CMyListCtrl that is of a pointer type of CMyProjectDialog, and then have CMyProjectDialog initialize this variable via a function in CMyListCtrl.
It looks like brazulewicz posted a similar method to my suggestion before my post went through.

I've had problems using brazulewicz method in the pass with VC++ 6.0.  It doesn't like it when you use "this" in the initialize list.

An alternative method could be the following:
class CMyProjectDialog;

class CMyListCtrl
{
private:
 CMyProjectDialog* parent;

public:
 CMyListCtrl():parent(NULL)
 {}
 Initialize_CMyProjectDialog(CMyProjectDialog* aParent)
 {
  parent = aParent;
 }
 sampleMethod()
 {
   parent->sampleDialogMethod();
 }
}

class CMyProjectDialog
{
private:
CMyListCtrl list;
public:
CMyProjectDialog();
sampleDialogMethod();
};

CMyProjectDialog::CMyProjectDialog()
{
 list.Initialize_CMyProjectDialog(this)
}
it seems to me you have soled your problem.
Thank you very much