1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

why C# doesn't support "friend" relationship?

Discussion in 'C#' started by Zeng, Sep 12, 2006.

  1. Zeng

    Zeng New Member

    Joined:
    Sep 12, 2006
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    It's so messy w/o the "friend" relationship. Does anyone know why it was
    not supported in C#. It's almost about as bad as it doesn't support the
    inheritance hierarchy and method reference (calling tree) browsing that is
    supported in C++. I don't know how some could write a large scale
    object-oriented application w/o those. If you have overcome these
    limitations in C#, please share your thoughts and ideas. Thanks!
     
  2. Essial

    Essial New Member

    Joined:
    Sep 12, 2006
    Messages:
    21
    Likes Received:
    0
    Trophy Points:
    0
    Can you give me some examples of C++ that C# cannot (As stated above)? I've been using C# for a while now, and the only problems I've ran into involve things that could possibly make the code unstable. And there has always been an alternate, safer, way of doing things. But perhaps I'm not quite understanding the question. Just give me some code samples of what you are talking about.
     
  3. Zeng

    Zeng New Member

    Joined:
    Sep 12, 2006
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    I am trying to write a class which no one can derive apart from my classes.

    Something like
    Code:
    class Usable;
    
    // cannot be derived apart from my known classes
    class Usable_lock {
    	friend class Usable;
    private:
    	Usable_lock() {}
    	Usable_lock(const Usable_lock&) {}
    };
    
    class Usable : public virtual Usable_lock {
    	// ...
    public:
    	Usable();
    	Usable(char*);
    	// ...
    };
    
    Usable a;
    
    class DD : public Usable { };
    
    DD dd;  
    // error: DD::DD() cannot access
    // Usable_lock::Usable_lock(): private  member
    
    I am sure this is possible in C# but I am not sure how. Can you help me in this regard.
     
  4. Essial

    Essial New Member

    Joined:
    Sep 12, 2006
    Messages:
    21
    Likes Received:
    0
    Trophy Points:
    0
    You can use the "internal" keyword. In C#, "internal" equates to allowing access to all classes that are in the same assembly as this class. There is no 1:1 support for "friend" but "internal" should be all you need. VisualStudio (and most C# editors) have code rollup and #region support, just use that to cut down your lines of code if your doing a lot of classes in it. So just declare your classes with "internal" perpended to it and everything should be peachy.
     
  5. Zeng

    Zeng New Member

    Joined:
    Sep 12, 2006
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Yes thats a good option. Infact the support of all the access specifiers is logistic

    public
    Access is not restricted.

    protected
    Access is limited to the containing class or types derived from the containing class.

    internal
    Access is limited to the current assembly.

    protected internal
    Access is limited to the current assembly or types derived from the containing class.

    private
    Access is limited to the containing type.
     

Share This Page