Link to home
Start Free TrialLog in
Avatar of joleinik
joleinik

asked on

Sub class needs to access parent's data member?

Hi all,
    I have a project that I'm working on and am completly stuck.  Here is the basic layout of my program:

I have a class that has in it's private members a hash table that has pointers to minHeaps.  In this same class, I have as a private member a maxHeap.  In my minHeap code, I need to add an item to my maxHeap.  I can't seem to find any possible way to do this!  Here is my class structure:

______________________________
HighwaySystem{
public:
   -add_tunnel(double height, int highway) - adds an item into one of the minHeaps in the hash table.
   -get_system_bottleneck() - pulls the first item in the maxHeap
   -numerous other functions.....

private:
  Highway* hArray[MAXITEMS];
  maxHeap M;
  other crap....
}

Highway{
public:

// Here's my problem!
  -hwyInsertTunnel( double tHeight) - inserts an item in the heap, if it's the smallest item in the   minHeap, then add it in the parent's maxHeap
  -a bunch of other functions...

private:

-minHeap H
-other crap...

______________________________

What is the best way to make it so that hwyInsertTunnel can access HighwaySystem's maxHeap?

Thanks a bunch!

-John Oleinik
Avatar of n_fortynine
n_fortynine

make Highway a friend of HighwaySystem.
Avatar of joleinik

ASKER

I have it as a friend, but the problem lies in that Highway is a data member of HighwaySystem.  If I have Highway as a friend, I would be able to be in the Highway class, create an instance of Highway class, and then be able to use it's private members.  But I'm trying the other way around.
   The only fix that I can think of would be to create a new constructor for Highway that takes a pointer to a HighwaySystem and then send it it address of "this" when I create it in the HighwaySystem code.  Is that legal in C++?  If so, how do I do it?
ASKER CERTIFIED SOLUTION
Avatar of efn
efn

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
Why don't you just write an access method?

A method to get the value of the private variable and a method to set the value of the private variable.
Avatar of Axter
>>I have it as a friend, but the problem lies in that Highway is a data member of HighwaySystem.  

How is that a problem?
SOLUTION
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
Thanks a bunch!
joleinik???
Yup.  John Oleinik.