Solved

Sub class needs to access parent's data member?

Posted on 2003-11-20
9
369 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
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!

 
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

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!

Question has a verified solution.

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

Suggested Solutions

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

734 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