Link to home
Start Free TrialLog in
Avatar of electrick
electrick

asked on

Pointers to class members

Say I have three classes.
CMyButton, CMyDialog, and CWhatEver.
CMyDialog has a member variable CPoint m_DlgPoint.
How would I retrieve the data in m_DlgPoint from either CMyButton or CWhatEver?
And then say I want to get a value stored in a member variable or function of CMyButton from CWhatEver?
and vice versa
Avatar of akalmani
akalmani

Hi !!
   Be a bit clear what are these classes i mean their base classes and what u want to assign from CMyButton class to CMyDialog class member.
assume U have three classes and U want to acces the memebr variable of each other then U can declare a friend function or
declare the object as global and use extern in other cpp files to access them
You should pay attention to one of the basic axioms of Object Oriented Programming, encapsulation.

Why do you need this value.

Do you want the CMyDialog to tell you where it is via a CPoint class object.

Then you might consider having a function like this:

BOOL CMyDialog::Location(CPoint& rPoint)
{
 BOOL success;

 // here you do some code to determien where this is, maybe somehting like :
 rPoint.x = curLocation.x;
 rPoint.y = curLocation.y

 // did we successfully find our location
 return success;
}

In this way, clients of CMyDialog need not know anything about the internal data of CMyDialog, but can still get the information they require
Hi,

 You have to be more specific in your question.
I will anyway guess.

Lets assume you have a dialog box of which CMyButton derived button is a member.

Coming to the CWhateverClass.
How is it related to the CDialog and CMyButton class?

Let me assume CWhateverClass object to be a member of CDialog.

class  CDialog::...
{
  CMyButton      m_BtnOk;
  CEdit          m_EditName;
  .
  .
  CWahteverClass m_WhatEver;


  ..
  ..
};

Then to get to get all accessible members of the CDialog Class the CMyButton class only has to do a GetParent()->m_MemberVariable.

Same for CWhatever object.

Now your problem was accessing CButtonClass members in CWhatever and CWhateverClass members in CButton.
Since both are unrelated tou can do it the following ways:

1. Provide interface functions to access the member function.
2. Pass the member function (if the value does not change like CWnd pointer) in the constructor of the class you need the member variable.
3. Make these variables as static variables. This is possible only in case of flags and such kind of variables and not for dynamic data which change from instance to instane.

Hope one of them is the required answer. If not give the proper hierarch and i will clear that.

Thanx
Rick Schneewind
Avatar of electrick

ASKER

Please forgive me, I'm used to doing things in VB, where life is much simpler.
I'm getting the feeling that what I want to do, really can't be done in all cases. I thought that I had explained it.  I was looking for a "one size fits all" type function that would work in every case.
So maybe it's best to just ask this...Is it possible to get a pointer, or somehow access, a member or function of another class, no matter what the class types are, or how they are related?
If so, How? If not, then OK.
ASKER CERTIFIED SOLUTION
Avatar of inpras
inpras

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