Solved

Sub class needs to access parent's data member?

Posted on 2003-11-20
9
363 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

863 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

25 Experts available now in Live!

Get 1:1 Help Now