473,662 Members | 2,406 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

can't access parent classes private member (VC6)

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) {}
void wii()
{
m_f.poo(); // #error C2248
}
} m_bar;

void poo() {}

public:
foo() : m_bar(*this) {}
};

error C2248: 'poo' : cannot access private member declared in class
'foo'
What can I doo?

--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%c gl%ssic%ccom%c" , "ma", 58, 'g', 64, "ba", 46, 10);}

_______________ _______________ __________
Looking for a good game? Do it yourself!
GLBasic - you can do
www.GLBasic.com
May 3 '06 #1
18 2962

Gernot Frisch wrote:
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) {}
void wii()
{
m_f.poo(); // #error C2248
}
} m_bar;

void poo() {}

public:
foo() : m_bar(*this) {}
};

error C2248: 'poo' : cannot access private member declared in class
'foo'
What can I doo?


You allow foo access to bar, but let bar access foo.

/Peter
[snip]

May 3 '06 #2
Gernot Frisch wrote:
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) {}
void wii()
{
m_f.poo(); // #error C2248
}
} m_bar;

void poo() {}

public:
foo() : m_bar(*this) {}
};

error C2248: 'poo' : cannot access private member declared in class
'foo'
What can I doo?


Declare classs bar as a friend of foo:

class foo
{
class bar; // forward-declaration of a member
friend class bar;

class bar
{
friend class foo;
// ^^^^^^^^^^^^^^^ ^
// do you really need this here?

foo & m_f;
public:
bar(foo & f) : m_f(f) {}
void wii()
{
m_f.poo(); // #error C2248
}
} m_bar;

void poo() {}

public:
foo() : m_bar(*this) {}
};

The other option is to abandon this outdated compiler for good.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 3 '06 #3
Gernot Frisch wrote:
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) {}
void wii()
{
m_f.poo(); // #error C2248
}
} m_bar;

void poo() {}

public:
foo() : m_bar(*this) {}
};

error C2248: 'poo' : cannot access private member declared in class
'foo'
What can I doo?


The same you can do to make any other class access that member: Make the
member public or make bar a friend of foo.

May 3 '06 #4

"Victor Bazarov" <v.********@com Acast.net> schrieb im Newsbeitrag
news:e3******** **@news.datemas .de...
Gernot Frisch wrote:
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) {}
void wii()
{
m_f.poo(); // #error C2248
}
} m_bar;

void poo() {}

public:
foo() : m_bar(*this) {}
};

error C2248: 'poo' : cannot access private member declared in class
'foo'
What can I doo?


Declare classs bar as a friend of foo:

class foo
{
class bar; // forward-declaration of a member
friend class bar;

class bar
{
friend class foo;
// ^^^^^^^^^^^^^^^ ^
// do you really need this here?

foo & m_f;
public:
bar(foo & f) : m_f(f) {}
void wii()
{
m_f.poo(); // #error C2248
}
} m_bar;

void poo() {}

public:
foo() : m_bar(*this) {}
};

The other option is to abandon this outdated compiler for good.


This works. But now I have more questions.
class foo
{
class bar;
friend class bar;
class bar{};
};

Now, foo can access bar's private members - am I wrong? I'm a bit
confused now.




May 3 '06 #5

Gernot Frisch wrote:
This works. But now I have more questions.
class foo
{
class bar;
friend class bar;
class bar{};
};

Now, foo can access bar's private members - am I wrong? I'm a bit
confused now.

No, it's the other way around. bar can now access foo's private (and
protected) members

Abdo Haji-Ali
Programmer
In|Framez

May 3 '06 #6

"Abdo Haji-Ali" <ah***@inframez .com> schrieb im Newsbeitrag
news:11******** **************@ v46g2000cwv.goo glegroups.com.. .

Gernot Frisch wrote:
This works. But now I have more questions.
class foo
{
class bar;
friend class bar;
class bar{};
};

Now, foo can access bar's private members - am I wrong? I'm a bit
confused now.

No, it's the other way around. bar can now access foo's private (and
protected) members


Thank you. I never had to use "friend" before.
May 3 '06 #7
> This works. But now I have more questions.
class foo < { class bar;
friend class bar;
class bar{};
};

Now, foo can access bar's private members - am I wrong? I'm a bit
confused now.


Declaring a class as your friend means that you let _them_ access
private members of _you_, not the other way around.

Think of it like this: I give you keys to my front door, so that you
have acess to my house. I declare you as my friend by giving you the
keys. You can declare me as your friend, but that doesn't give you
access to my house. Only I can say whether you have access or not.

class A {};
class B { friend class A }; <-- A can access private members of B

Lyell

May 3 '06 #8
Think of it like this: I give you keys to my front door, so that you
have acess to my house. I declare you as my friend by giving you the
keys. You can declare me as your friend, but that doesn't give you
access to my house. Only I can say whether you have access or not.


That's nice to remember for good.
May 3 '06 #9
Gernot Frisch wrote:
"Abdo Haji-Ali" <ah***@inframez .com> schrieb im Newsbeitrag
news:11******** **************@ v46g2000cwv.goo glegroups.com.. .
Gernot Frisch wrote:
This works. But now I have more questions.
class foo
{
class bar;
friend class bar;
class bar{};
};

Now, foo can access bar's private members - am I wrong? I'm a bit
confused now.

No, it's the other way around. bar can now access foo's private (and
protected) members


Thank you. I never had to use "friend" before.


Easy way to remember:

Only you and your friends can play with your private parts.
May 3 '06 #10

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

Similar topics

7
1799
by: Eternally | last post by:
Ok, this may sound confusing....but it's really simple. If you're confused, just look at my example code and it'll make sense. Here's my situation. I have 2 classes....A and B. Class A has a member variable of type B. Class B has a member function which does calculations which are dependent upon values of members of the Class A that owns it. When in that function of class B, I don't know how I can gain access to it's
3
21382
by: Rajesh Garg | last post by:
Can we have private constructors and destructors? IF yes what is the use of such constructors or destructors.....in the sense where can these be implemented in a system................. I have an idea that we can have private constructors and destructors but am not able to find a situation where they can be used... Regards RVG rajeshgarg@opussoft.com
7
9758
by: Wolfgang Jeltsch | last post by:
Hello, I want to write a list class with an iterator class as an inner class. The iterator class must have access to certain private members of the list class in order to do its job. Here is a reduced code example: class List { private: void *rootNode; class Iterator {
5
1844
by: William Payne | last post by:
Hello, consider the following two classes (parent and child): #ifndef SINGLETON_HPP #define SINGLETON_HPP #include <cstddef> /* NULL */ template <typename T> class Singleton {
12
2667
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;
22
23347
by: Matthew Louden | last post by:
I want to know why C# doesnt support multiple inheritance? But why we can inherit multiple interfaces instead? I know this is the rule, but I dont understand why. Can anyone give me some concrete examples?
3
9824
by: Sushil Srivastava | last post by:
Hi Guys, Would you be able to help me using C# GUI (with user interface component) in my MFC application. I have used managed extension, COM-interops, etc but problem is this C# component has user interface. how should have get window handle from managed windows? Thanks in advance. Sushil
5
1909
by: Just Me | last post by:
Given a button name Btn_5 and Index=5 I want to do something like dim zz as string = Btn_??Index??.Text or given an array of buttons, do:
6
1761
by: Peter Oliphant | last post by:
I just discovered that the ImageList class can't be inherited. Why? What could go wrong? I can invision a case where someone would like to add, say, an ID field to an ImageList, possible so that the individual elements in an array of ImageList's could be identified by the ID, thereby allowing re-ordering the array without harm. A person could identify by index into the array, but that would not be preserved by re-ordering (and re-ordering...
0
8432
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
8343
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
8762
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
8545
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
8633
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
5653
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();...
0
4179
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...
0
4347
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1747
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.