473,785 Members | 2,488 Online
Bytes | Software Development & Data Engineering Community
+ 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 2039
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
3945
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 private members of nesting class. template <typename T> class Container { // NO friendship given to any other public: class ContainerIterator;
12
3256
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 private. I have placed "friend void CA::Run_A(void)" in CMain Class. CMain::Run() function attempts to execute CA::Run_A(), but compiler shows an error saying that it is the violation to access private function. I don't understand why because friend...
5
6567
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
1954
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 ostream & operator<<(ostream & x, const MyClass & c); }
12
2680
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 ) {cout<<"a.value="<<a.value<<endl; } private: int value;
18
2971
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
1704
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 class basis, not on an instance by instance basis'. I couldn't quite believe it, and tried it out for myself, and was shocked to see it was true... What is the reasoning behind this? Why should one Person object be able to see another Person...
5
3569
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 me to links to read, I just cannot seem to find anything about this, maybe it's not possible. Thanks in advance, jmy
11
6447
by: Yarco | last post by:
For example: <?php class Test { private $name = 'yarco'; } $p = new ReflectionPropery('Test', 'name'); print $p->getValue();
0
9645
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
9480
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
10325
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
10147
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
10091
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
9950
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
8972
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
4050
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
2
3645
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.