CFanatic

Go Back   CFanatic > Programming > C# Programming

Join CFanatic Forum Now

why C# doesn't support "friend" relationship? topic posted under C# Programming which is a part of Programming category in CFanatic Forum
Reply
 
Thread Tools Display Modes
  #1  
Old 09-12-2006, 12:01 AM
Junior Member
 
Join Date: Sep 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Zeng is on a distinguished road
| More
why C# doesn't support "friend" relationship?

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!
Reply With Quote
  #2  
Old 09-12-2006, 07:45 AM
Junior Member
 
Join Date: Sep 2006
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Essial is on a distinguished road
| More
Re: why C# doesn't support "friend" relationship?

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.
Reply With Quote
  #3  
Old 09-12-2006, 09:06 AM
Junior Member
 
Join Date: Sep 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Zeng is on a distinguished road
| More
Re: why C# doesn't support "friend" relationship?

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.
Reply With Quote
  #4  
Old 09-12-2006, 09:12 AM
Junior Member
 
Join Date: Sep 2006
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Essial is on a distinguished road
| More
Re: why C# doesn't support "friend" relationship?

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.
Reply With Quote
  #5  
Old 09-12-2006, 12:11 PM
Junior Member
 
Join Date: Sep 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Zeng is on a distinguished road
| More
Re: why C# doesn't support "friend" relationship?

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.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
RE:" THE BOUNCING BALL in C++" lucamarcus C++ Programming 0 10-21-2007 03:16 PM
Error The text "*" is unexpected kaustubh.deo C++ Programming 0 09-30-2006 06:39 PM
Windows 2003 Support AmilHanish Win32 Programming 0 09-29-2006 06:52 AM

 

Advertisement

CFanatic Search
Custom Search

Advertisement

All times are GMT -5. The time now is 03:05 PM.



Powered by vBulletin® Version 3.7.4
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO ©2010, Crawlability, Inc.
Cfanatic.com is a premier member of the IDG TechNetwork. For advertising opportunities contact here
Get Paid for Working on Projects Matching Your Expertise at Go4Expert's Jobs Board