C++ friend class equivalent

D

Davis

Hi, In VB if i want to define a class that has access to private
elements of another class how do i declare it. In C++ you
just use friend keyword.

As far as i understand the Friend keyword in VB allows that
particular element to be accessed outside the class but within an assembly
which is not what i'm after.

Thanks
 
D

Davis

Hi Chris, the internal keyword is equivalent to Friend in VB but
this is not what i want. internal allows that class or method
to be accessed outside the class but within the assembly.

I want to give an external class access to private methods in
my class, just as the friend keyword does in C++

c++ class

ie class A{
friend class B;
-----
}

here class B has access to all of A's private members and methods.

'internal' alows a member or method to be accessed outside class A

c# class

class A{
internal int var;
}

so its just a restricted version of public

I am new to CF but as AFAIK internal (C#) or Friend(VB)
is not the same as friend i nC++ which is what i want.

Also i don't want to make all my members and methods public or internal in
my class.

Hope this clarifies for you.
 
N

Norman Rericha

The issue here is that you are trying to access private functionality
outside a given assembly. AFAIK the only way this happens in managed
code is via PUBLIC. You could inherit the base and use PROTECTED to
allow exposure to the private functions. Private declarations will not
be available via inheritance.

I would question the design/read-ability/maintain-ability of your goal.
I realize that friend in C++ allows an "expansion" for encapsulation.
From my experience it basically allowed you to use similar code from an
existing class. You can do this with inheritance.


class B
{
protected int MyInt;

public B()
{
this.MyInt = 53;
}

protected int Increase(int Value)
{
return Value++;
}
}

class A : B
{
public int TheNewInt;

public A() : B()
{
this.TheNewInt = Increase(this.MyInt);
}
}

Of course if B is just a "template" you could use abstract so that it
can only be inherited. Note that only public, internal, protected
methods/properties will be available to A. Keeps the code nice and
neat.

--
I hope this helps

--
Norman Rericha
Senior Software Engineer
Applied Data Systems
www.applieddata.net
An ISO 9001:2000 Registered Company
Microsoft WEP Gold-level Member
 
D

Davis

Hi Norman, basically what i am doing is i have a unit test dll
that i want to test a class library which i want t keep separate.

If there was somthing equivalent to friend in C++ all i had to do
was declare my test class to be a 'friend' of my class to be tested
and everything is easy.

Now i am stuck as to how to proceed with this, any ideas
 
P

Paul G. Tobey [eMVP]

In a word, "reflection". Reflection is also less intrusive than declaring a
bunch of friend classes, thus creating a parasitic dependency of your actual
code on the name of the test class.

Paul T.
 
G

Guest

Now that we know what you're trying to truly do it's a bit simpler to
answer.

You have two options:

1. Use reflection to load the methods - it can load even private methods.
2. If you strong-name the assemblies, in the callee you can add a
'System.Runtime.CompilerServices.InternalsVisibleTo' assembly attribute and
directly give the caller permission. We do that in the SDF.
 
N

Norman Rericha

As previously mentioned ... you want to use reflection for the best
results.

--
I hope this helps

--
Norman Rericha
Senior Software Engineer
Applied Data Systems
www.applieddata.net
An ISO 9001:2000 Registered Company
Microsoft WEP Gold-level Member
 
D

Davis

Hi Chris, yes sorry i should have said at beginning. Many thanks
for the answers to all.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

C# event Handles 3
Friend Class 8
friend class in c# 3
C++ 'friend' keyword 3
C++ type friend functionality in C# 4
"friend" level access 19
Friend Class 2
Internal vs friend (C++) 7

Top