473,466 Members | 1,613 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Private data access using friend

I am working on a code which is similar to the following snippet:

#include <iostream>

using namespace std;

class def;

class abc
{
friend class def;
private:
int p;
};

class def
{
public:
void gg()
{
abc a1;
a1.p = 20;
}
};

int main()
{
def d1;
}

I apologise for the cryptic names of the classes. My doubt is how can I
access the private member p of class abc in gg() method of def class.
Friend class can access the private data of the other class but here I
am trying to acess the private data using an object. Is that valid ?
The code compiles fine on VC 6.0. I know VC6.0 is not a compiler one
should be using if he needs to learn C++ but since I am working on
mobile applications, I have to use VC6.0.

Is this a case of a broken compiler ? I do not think so because the
code also compiles fine on Metrowerks Codewarrior which is based on
gcc.

Please let me know.

Mar 10 '06 #1
1 2018
Jaspreet wrote:
I am working on a code which is similar to the following snippet:

#include <iostream>

using namespace std;

class def;
Everything up until here is unnecessary in your particular program. Try
not to include extraneous stuff in your code. It makes it much more
readable.

class abc
{
friend class def;
private:
int p;
};

class def
{
public:
void gg()
{
abc a1;
a1.p = 20;
}
};

int main()
{
def d1;
}

I apologise for the cryptic names of the classes. My doubt is how can I
access the private member p of class abc in gg() method of def class.
Why do you doubt that?
Friend class can access the private data of the other class
That includes anything that is declared private.
but here I
am trying to acess the private data using an object. Is that valid ?
Of course!
The code compiles fine on VC 6.0. I know VC6.0 is not a compiler one
should be using if he needs to learn C++ but since I am working on
mobile applications, I have to use VC6.0.
For testing your code you can always turn to VC++ v8 Express Edition,
which is free and much closer to the Standard, or to the online compiler
provided by Comeau Computing (www.comeaucomputing.com/tryitout).
Is this a case of a broken compiler ? I do not think so because the
code also compiles fine on Metrowerks Codewarrior which is based on
gcc.


I hope I have.

V
--
Please remove capital As from my address when replying by mail
Mar 10 '06 #2

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

Similar topics

8
by: CoolPint | last post by:
I read in books that nested class cannot access private members of nesting class and vice versa unless they are made friends. Somehow, my compiler is letting my nested class member functions access...
12
by: Bryan Parkoff | last post by:
CMain Class is the base class that is initialized in main function. CA Class is the base class that is initialized in CMain::CMain(). CMain Class is always public while CA Class is always...
5
by: Aakash Joshi | last post by:
is there any way we can access private member function of a class from outside. i know we can access private member data using void pointer
5
by: JustSomeGuy | last post by:
I have a class with a private data member and I want to access it from a friend function, but Visual Studio 2003 .NET won't let me. class MyClass { private: static int level; public: friend...
12
by: Manolis | last post by:
Hi, I was wondering if there is any way to make two objects of the same class to be able to access each other's private data, like this: class A { public: void access( const A& a )...
18
by: Gernot Frisch | last post by:
This code works on VC7.1. But VC6 refuses to compile. class foo { class bar { friend class foo; foo & m_f; public: bar(foo & f) : m_f(f) {}
6
by: Siam | last post by:
Hi all, I read somewhere that private members of some instance of class A are accessible from another instance of the same class. In other words, access to private members is done 'on a class by...
5
by: JamesO | last post by:
Hello all, Is there a way to make a class private to the namespace so that other classes in the namespace can see and use it but noone outside a namespace can see or use it? Feel free to send...
11
by: Yarco | last post by:
For example: <?php class Test { private $name = 'yarco'; } $p = new ReflectionPropery('Test', 'name'); print $p->getValue();
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
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...
1
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...
0
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,...
0
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...
0
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 ...

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.