CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2002
    Posts
    23

    friend class access

    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.

  2. #2
    Join Date
    Jun 2002
    Posts
    1,417
    Here is one solution:

    Code:
    class A
    {
    	friend class B;
    	private:
    	struct node {
    	//some variables in here
    	};
    
    };
    
    class B
    {
    private:
    	A::node *current;
    
    };

  3. #3
    Join Date
    Oct 2002
    Posts
    36
    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?

    Code:
    #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 ..depends on you

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured