473,698 Members | 2,246 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C++ 'friend' keyword

Hi,

I'm looking for an equivalent to the C++ 'friend' keyword in C# (for those
who don't know, this lets you give a specific class access to all the
private/protected members of another class).

I'm trying to write an object persistence mechanism where all of the
persistence logic for each class is situated in a seperate class (the Mapper
design pattern). The object itself is completely unaware of how it is being
persisted. This lets someone in the future write a new persistence layer
(e.g. to a different DBMS) without modifying the object code. The difficulty
is that this persistence class needs priviledged access to the class it is
trying to persist (i.e. it needs to set/get private members which ordinarily
shouldn't be accessed by code outside the class). In C++ you can do this
with the 'friend' keyword.

I'm aware you can do this with the 'internal' keyword, however this seems
much too coarse. I don't want to allow every piece of code in the assembly
to mess around with the internals of every other class. I don't want to use
a 3rd party object persistence framework as it needs to be very fast, and I
need a lot of control over it.

Is there any other way of doing this cleanly, or plans to allow this in C#
2.0?

Thanks,

Ben
Nov 16 '05 #1
3 2962
"internal" keyword is replacement for "friend" in c#.

internal void myFunction()
{

}

--
Shak
(Houston)
"Ben Galvin" <bg*****@fastma il.fm> wrote in message
news:eD******** ******@TK2MSFTN GP10.phx.gbl...
Hi,

I'm looking for an equivalent to the C++ 'friend' keyword in C# (for those
who don't know, this lets you give a specific class access to all the
private/protected members of another class).

I'm trying to write an object persistence mechanism where all of the
persistence logic for each class is situated in a seperate class (the Mapper design pattern). The object itself is completely unaware of how it is being persisted. This lets someone in the future write a new persistence layer
(e.g. to a different DBMS) without modifying the object code. The difficulty is that this persistence class needs priviledged access to the class it is
trying to persist (i.e. it needs to set/get private members which ordinarily shouldn't be accessed by code outside the class). In C++ you can do this
with the 'friend' keyword.

I'm aware you can do this with the 'internal' keyword, however this seems
much too coarse. I don't want to allow every piece of code in the assembly
to mess around with the internals of every other class. I don't want to use a 3rd party object persistence framework as it needs to be very fast, and I need a lot of control over it.

Is there any other way of doing this cleanly, or plans to allow this in C#
2.0?

Thanks,

Ben

Nov 16 '05 #2
On 15 Jul 2004 05:28, "Ben Galvin" wrote:
Hi,

I'm looking for an equivalent to the C++ 'friend' keyword in C# (for those
who don't know, this lets you give a specific class access to all the
private/protected members of another class).

I'm trying to write an object persistence mechanism where all of the
persistence logic for each class is situated in a seperate class (the Mapper
design pattern). The object itself is completely unaware of how it is being
persisted. This lets someone in the future write a new persistence layer
(e.g. to a different DBMS) without modifying the object code. The difficulty
is that this persistence class needs priviledged access to the class it is
trying to persist (i.e. it needs to set/get private members which ordinarily
shouldn't be accessed by code outside the class). In C++ you can do this
with the 'friend' keyword.

I'm aware you can do this with the 'internal' keyword, however this seems
much too coarse. I don't want to allow every piece of code in the assembly
to mess around with the internals of every other class. I don't want to use
a 3rd party object persistence framework as it needs to be very fast, and I
need a lot of control over it.

Is there any other way of doing this cleanly, or plans to allow this in C#
2.0?

Thanks,

Ben


Perhaps sadly, there is no exact equivalent: 'internal' is as close as
you can get.
However, a class defined within another class, such as:

public class Outer {
.....
public class Inner {
private Inner (Outer myOuter) {...}
}
}

has access to the privates of the outer defining class. You could also
add a constructor to Inner which takes an Outer (probably the instance
of Outer which contains it). This is how the GoF Memento pattern would
be implemented in C# and might be of help to you.
Other approaches I have seen include having constructors which take a
DataRow (for creation) and providing a method which either returns XML
(or some similar structure) or a DataRow for something else to use for
insert/update.
My own experience is that for reasonable performance and simplicity the
class itself[1] generates the SQL required for inserts/updates, which is
passed to something else for execution. If you possibly have different
RDBMS's as the backend this is an obvious case for the Strategy pattern.
This - to me - also ends up with the simplest structures. Sure, it pollutes
the classes with persistence code but OR is a difficult problem and you'll
find it very, very, hard to come up with a design which at some point doesn't
have to loose some sort of 'OO purity'. There are some good articles about
it on Scott Ambler's site (I think some of them have been withdrawn - he
want's you to buy his book!) at http://www.ambysoft.com/persistenceLayer.html
[1] I regard an Inner class as I mentioned to be really part of the Outer,
containing class. Delegating the SQL generation to that class helps in
making clear that the SQL stuff is not part of the main purpose of Outer
and makes it look slightly cleaner.

--
Simon Smith
simon dot s at ghytred dot com
www.ghytred.com/NewsLook - NNTP Client for Outlook
Nov 16 '05 #3
Not exactly the same as C++ friends but Whidbey allows you to have a friend
assembly. See Omer's blog on this for more details.

http://weblogs.asp.net/okloeten/arch.../01/35176.aspx

Regards
Ram

"Ben Galvin" <bg*****@fastma il.fm> wrote in message
news:eD******** ******@TK2MSFTN GP10.phx.gbl...
Hi,

I'm looking for an equivalent to the C++ 'friend' keyword in C# (for those
who don't know, this lets you give a specific class access to all the
private/protected members of another class).

I'm trying to write an object persistence mechanism where all of the
persistence logic for each class is situated in a seperate class (the Mapper design pattern). The object itself is completely unaware of how it is being persisted. This lets someone in the future write a new persistence layer
(e.g. to a different DBMS) without modifying the object code. The difficulty is that this persistence class needs priviledged access to the class it is
trying to persist (i.e. it needs to set/get private members which ordinarily shouldn't be accessed by code outside the class). In C++ you can do this
with the 'friend' keyword.

I'm aware you can do this with the 'internal' keyword, however this seems
much too coarse. I don't want to allow every piece of code in the assembly
to mess around with the internals of every other class. I don't want to use a 3rd party object persistence framework as it needs to be very fast, and I need a lot of control over it.

Is there any other way of doing this cleanly, or plans to allow this in C#
2.0?

Thanks,

Ben

Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2599
by: Ike Naar | last post by:
Given the following C++ snippet: template < typename T > class A { // 1 public : // 2 class AA { } ; // 3 } ; // 4 // 5 template < typename T > class B { // 6 friend class A < T > :: AA ; // 7 } ; // 8
5
2126
by: Teddy | last post by:
Hello all consider the class Date declaretion below: class Date { public: Date(); Date(int year, int month, int day); Date(const string&); int getYear() const;
8
1188
by: John Young | last post by:
Hi, I have a class (cGlobals.cs) which I create in my main form (frmMain). I have declared a variable at the top of my frmMain class (cGlobals gVars;). I then create an instance in my forms Load event (gVars=new cGlobals;). Now, I can access gVars in my main form, but not from any other form. Do I need to add anything to my cGlobals.cs file to allow all forms to access the class? I've seen references to friend/internal etc., but am...
7
13985
by: Steve | last post by:
I'm just curious, why did give VB.Net a Friend keyword but not C#?
7
9816
by: Jesper | last post by:
I need to grant a class access to protected fields of another class in the way its possible in C++ with the friend keyword. However I would like to keep the class protected towards other class within the same program/assembly. The two classes are not 'related' (inherited). How can I do this. I cant see how the keyword Internal can be used for this purpose.
4
1203
by: Howard Swope | last post by:
I keep running into places in my code where I would like only a particular class of object to be able to access certain members of another class of object. In C++ the friend keyword provided this functionality. The only thing I have found in C# is internal but this is still broader in scope than I am looking for. Is there a way in C# to get the functionality of the C++ friend keyword? -- Howard Swope
8
13726
by: Paul Cheetham | last post by:
Hi, I am writing an application with a large number of various classes, and I want some of them to have Friend access to protected members of other classes. i.e. I want class A to have access to internal members of Class B In C++ this was simple, as in the definition for class B, I would declare calss A as a friend. Seeing that C# had a friend keyword I thought this was going to be easy,
3
3490
by: Filimon Roukoutakis | last post by:
Dear all, I have the following concept name { space1 { class Friend { };
2
2427
by: Immortal Nephi | last post by:
I design a class for general purpose. I do not allow a client to read or modify interface and implemention. I allow them to write a new non- member function outside of class' interface and implmention. The problem is that non-member function cannot access private data member. The friend keyword is not the solution. I am aware that friend keyword allows the non-member function to access private data member. If you define more than...
0
8683
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8609
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9170
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9031
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8901
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8871
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7739
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4371
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.