Create a pointer to an existing Object....

Belthazor
Belthazor used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
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 ...

Author

Commented:
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.
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

so we have:
/--
class CMyProjectDialog;

class CMyListCtrl
{
private:
  CMyProjectDialog* parent;

public:
  CMyListCtrl(CMyProjectDialog* aParent);
  void sampleMethod();
}

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


CMyListCtrl::CMyListCtrl(CMyProjectDialog* aParent)
 : parent(aParent)
{}

CMyListCtrl::sampleMethod()
{
  parent->sampleDialogMethod();
}

CMyProjectDialog::CMyProjectDialog()
  : list(this)
{}

should work

Commented:
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.

Commented:
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)
}

Commented:
it seems to me you have soled your problem.

Author

Commented:
Thank you very much

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial