Click to See Complete Forum and Search --> : friend class access
Lil'Hasher
November 22nd, 2002, 10:14 PM
Hi i have some code similar to below but i cant seem to get it to work as i would like. I'm wondering if what i'm doing is invalid. (ie. trying to use the struct defined in one class through its friend)
Class A{
friend class B;
private:
struct node{
//some variables in here
}
.....
}
Class B{
private
node *current;
....
}
I get an error implying that the current variable is not defined/recognized.
The only other way i can think of doing this is making a Class node but i think a struct is better and would like to do it this way if possible.
stober
November 23rd, 2002, 12:46 AM
Here is one solution:
class A
{
friend class B;
private:
struct node {
//some variables in here
};
};
class B
{
private:
A::node *current;
};
dumah
November 23rd, 2002, 02:26 PM
Have you thought that perhaps if B needs node, then it shouldnt be tucked away in A and accessed via the "friend" declaration?
If you want both classes to use this node, then I dont see why 1 should have "ownership"
Maybe if they both share usage, this could be pushed down to a base class?
#include <iostream>
class SomeInterface{
protected://protected so only inheriting classes can use it
struct node {
//node does whatever
void Action(){std::cout << "Does something" << std::endl;}
};
};
class A: public SomeInterface{
public:
void DoWhatever(){m_node.Action();}//standard function for class
private:
node m_node;//class can now use nodes
};
class B: public SomeInterface{
public:
void DoWhatever(){m_node.Action();} //standard function for class
private:
node m_node;//class can now use nodes
};
int main(){
A a;
B b;
//Now each class can use node internally without
// trusting each other as frinds
a.DoWhatever();
b.DoWhatever();
}
Of course I am taking wild assumptions at your class designs and usages....this may be an idea...it may not :confused: ..depends on you
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.