473,597 Members | 2,459 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

friend class

Dear all,
I would like to have an access from one class to the private variables of
other. But I also can't find a right sintax. The word "friend" seems to do
something else, VB compiler still complains.
-----C++--------

class A{
private:
int K;
}
class B{
friend class A;

void test(){
A oA;
A.K=10; //acccess private member
}
}
------VB----
Please help
Nov 21 '05 #1
4 1985
"Boni" <oilia@nospam > schrieb:
I would like to have an access from one class to the private variables of
other. But I also can't find a right sintax. The word "friend" seems to do
something else, VB compiler still complains.
-----C++--------

class A{
private:
int K;
}
class B{
friend class A;

void test(){
A oA;
A.K=10; //acccess private member
}
}


That's not supported.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #2
Is there a good solution?
I would like to have most class members private for all classes except for
one special, which has special handlers. What is a good design style to do
so?
"Herfried K. Wagner [MVP]" <hi************ ***@gmx.at> schrieb im Newsbeitrag
news:OD******** ******@TK2MSFTN GP12.phx.gbl...
"Boni" <oilia@nospam > schrieb:
I would like to have an access from one class to the private variables of
other. But I also can't find a right sintax. The word "friend" seems to
do something else, VB compiler still complains.
-----C++--------

class A{
private:
int K;
}
class B{
friend class A;

void test(){
A oA;
A.K=10; //acccess private member
}
}


That's not supported.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #3

"Boni" <oilia@nospam > wrote in message
news:Oy******** ******@TK2MSFTN GP15.phx.gbl...
Is there a good solution?
I would like to have most class members private for all classes except for
one special, which has special handlers. What is a good design style to do
so?


A similar way?

Friend Class MyClass1
Friend Name As String
Friend Address As String
End Class

Public Class MyClass2
Private mMyClass As MyClass1

Public Sub New()
mMyClass = New MyClass1()
End Sub

Public Property Name() As String
Get
Return mMyClass.Name
End Get
Set
mMyClass.Name = Value
End Set
End Property

Public Sub SetAddress(ByVa l Address As String)
mMyClass.Addres s = Address
End Sub
End Class
The difference is, any class in the assembly can access MyClass1 and it's
properties/methods. But, everything outside the assembly can't access
MyClass1 directly.

Mythran

Nov 21 '05 #4

"Boni" <oilia@nospam > wrote in message
news:er******** ******@TK2MSFTN GP15.phx.gbl...
I would like to have an access from one class to the private
variables of other.


You can't. If you need to be access a variable outside of a given
class, it has to be exposed, probably via a property.

Looking at your code, I would tend towards adding a Friend
method called something like "Prime", which class *in the same
assembly* can use to pass in a value, as in

Class A
Private K as Integer = Integer.MinValu e

Friend Sub Prime( ByVal Value As Integer )
' Prevent Prime being called more than once
If K <> Integer.MinValu e Then
Throw New ApplicationExce ption()
End If

K = Value
End Sub

End Class

Class B
Private oA As New A

Sub New()
oA.Prime( 10 )
End Sub

End Class

HTH,
Phill W.
Nov 21 '05 #5

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

Similar topics

2
12517
by: Christophe Barbe | last post by:
I am not clear about friend functions of a template class. GCC (3.3.2) wants me to add <> after the friend function names in the class declaration and VisualC++ doesn't like that. template <class T> class test{ test(void); ~test(void); friend bool operator== <> (const test<T> &p1, const test<T> &p2); }
1
4434
by: Dmitry D | last post by:
Hi all, I'm having problems with declaring a template friend function. It seems like I've done everything as explained in C++ FAQ, but still, I'm getting the linker error (unresolved external 'aFunc(A<char> const&)' ) in the following sample program. Tried it on GCC and Borland's compiler. Can somebody please show me the correct way to fix this? Thanks, Dmitry
5
2695
by: Trevor Lango | last post by:
What is the appropriate syntax for placing a friend function that includes as one of it's parameters a pointer to the class object itself within the template class? I have the following: //**************************************************** // testClass.h //**************************************************** #ifndef TESTCLASS_H
15
6575
by: Samee Zahur | last post by:
Question: How do friend functions and static member functions differ in terms of functionality? I mean, neither necessarily needs an object of the class to be created before they are called and either has access only to static members of the class (ie. assuming no object of the class is in scope - neither by arguments recieved nor by local declarations). Any static member function like this: //accessing static member i static void...
9
3725
by: Adam Badura | last post by:
I have code like this template<int size> big_int { /* ... */ template<int size2> friend class big_int<size2>; }; What I wanted to achive is to be able to easly convert different sized big_int's and to operate on them. But for this I need to be able to access private data of big_int. The problem is that for differenc sizes this are of
2
2044
by: freegnu | last post by:
how to declare a friend function that can access two class it will look like the following class A { private: int i; public: A(){} ~A(){} friend void call(A &a, B &b);
6
1725
by: ralphmerridew | last post by:
My current project involves reading a particular data format. It would be convenient to be able to do something like: class DataClass { friend bool FileRunner::read_data (const vector<string>&); ... }; class OtherDataClass {
3
3486
by: Filimon Roukoutakis | last post by:
Dear all, I have the following concept name { space1 { class Friend { };
5
2150
by: Amit Gupta | last post by:
How do I do it (either by friend or by any other hack). I have three class, I am just writing derivation below: class A{} class B{} class C : public class B{} class D: public class B ()
6
3158
by: WaterWalk | last post by:
I find friend declaration just very tricky. I tried the following examples on both MingW(gcc 3.4.2) and VC++ 2005. The results are surprising. Example1: namespace ns1 { class Test { friend void func()
0
7893
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
8276
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...
1
8040
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
8259
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
6698
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...
1
5847
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5436
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2408
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1495
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.