Solved

Sub class needs to access parent's data member?

Posted on 2003-11-20
9
367 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

762 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