Solved

Sub class needs to access parent's data member?

Posted on 2003-11-20
9
362 Views
Last Modified: 2011-09-20
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
0
Comment
Question by:joleinik
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 4

Expert Comment

by:n_fortynine
ID: 9795048
make Highway a friend of HighwaySystem.
0
 

Author Comment

by:joleinik
ID: 9795080
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?
0
 
LVL 15

Accepted Solution

by:
efn earned 250 total points
ID: 9795164
Sure, it's legal.

class Highway
{
  Highway(HighwaySystem* system);
  ...
};

Because you have two classes that refer to each other with pointers, you will have to use forward declarations.

--efn
0
 
LVL 8

Expert Comment

by:_corey_
ID: 9795586
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.
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 30

Expert Comment

by:Axter
ID: 9795706
>>I have it as a friend, but the problem lies in that Highway is a data member of HighwaySystem.  

How is that a problem?
0
 
LVL 30

Assisted Solution

by:Axter
Axter earned 250 total points
ID: 9795717
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.....
  friend class Highway;//  ****friend*****
private:
 Highway* hArray[MAXITEMS];
 maxHeap M;
 other crap....
};

Even if Highway is a data member, it should still work if you make it a friend.
0
 

Author Comment

by:joleinik
ID: 9795836
Thanks a bunch!
0
 
LVL 4

Expert Comment

by:n_fortynine
ID: 9797753
joleinik???
0
 

Author Comment

by:joleinik
ID: 9798478
Yup.  John Oleinik.  
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now