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
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
make Highway a friend of HighwaySystem.
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?
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
A method to get the value of the private variable and a method to set the value of the private variable.
>>I have it as a friend, but the problem lies in that Highway is a data member of HighwaySystem.
How is that a problem?
How is that a problem?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks a bunch!
joleinik???
ASKER
Yup. John Oleinik.