473,508 Members | 2,361 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%cgl%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 2947

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.********@comAcast.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.googlegr oups.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.googlegr oups.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
red floyd wrote:
[..]
Easy way to remember:

Only you and your friends can play with your private parts.


You let your friends do that? Do they enjoy it? Eek!
May 3 '06 #11
Victor Bazarov wrote:
red floyd wrote:
[..]
Easy way to remember:

Only you and your friends can play with your private parts.


You let your friends do that? Do they enjoy it? Eek!


Only my female friends :) Of course, Mrs. red floyd doesn't need to
know about this, does she?
May 3 '06 #12
red floyd wrote:
Victor Bazarov wrote:
red floyd wrote:
[..]
Easy way to remember:

Only you and your friends can play with your private parts.


You let your friends do that? Do they enjoy it? Eek!


Only my female friends :) Of course, Mrs. red floyd doesn't need to
know about this, does she?


My lips are private.
May 3 '06 #13
Gernot Frisch wrote:
This code works on VC7.1. But VC6 refuses to compile.

I faced similar issue with different versions of gcc.
There seems to be a change in c++ standard which makes Nested class a
friend of Nestee by default.
VC7.1 is probably compliant to the latest standard while VC6 is not.
See : http://gcc.gnu.org/ml/gcc-bugs/2005-06/msg00458.html

May 4 '06 #14
am****@gmail.com wrote:
There seems to be a change in c++ standard which makes Nested class a
friend of Nestee by default.


Could you cite a reference for this please? In ISO/IEC 14882:2003 there
is an example in 11.8.1 (yes yes I know the examples are
non-normative):

class E {
int x;
class B { };
class I {
// error: E::B is private
B b;
int y;
void f(E* p, int i)
{
// error: E::x is private
p->x = i;
}
};
int g(I* p)
{
// error: I::y is private
return p->y;
}
};
Well of the three supposed 'errors' above, only the final one is
considered an error by any of the modern compilers! Where did this
change happen?

Thanks...

May 4 '06 #15
Pete C wrote:
am****@gmail.com wrote:
There seems to be a change in c++ standard which makes Nested class a
friend of Nestee by default.

Could you cite a reference for this please? In ISO/IEC 14882:2003 there
is an example in 11.8.1 (yes yes I know the examples are
non-normative):

class E {
int x;
class B { };
class I {
// error: E::B is private
B b;
int y;
void f(E* p, int i)
{
// error: E::x is private
p->x = i;
}
};
int g(I* p)
{
// error: I::y is private
return p->y;
}
};
Well of the three supposed 'errors' above, only the final one is
considered an error by any of the modern compilers! Where did this
change happen?

It hasn't yet, it's a proposed change that most compiler have implemented.

--
Ian Collins.
May 4 '06 #16
According to Wolfgang Bangerth in the link I posted: "It is part of the
Technical Corrigendum TC1 of the standard, however, and therefore
(retroactively) considered part of C++98"

I am curious to know that if some thing goes into a TC is it equivalent
to being the standard or is it still in the 'proposed' status.
It's important to know because I have migrated to a higher version of
the compiler rather than changing my code.

Thanks,
-A

May 4 '06 #17
am****@gmail.com wrote:
According to Wolfgang Bangerth in the link I posted: "It is part of the
Technical Corrigendum TC1 of the standard, however, and therefore
(retroactively) considered part of C++98"

I am curious to know that if some thing goes into a TC is it equivalent
to being the standard or is it still in the 'proposed' status.
It's important to know because I have migrated to a higher version of
the compiler rather than changing my code.

Please see <http://cfaj.freeshell.org/google/> for posting guidelines.

It was a feature implemented by a number of compilers (probably first in
gcc?) that doesn't break existing code, so other vendors have followed
in the name of compatibility :)

--
Ian Collins.
May 4 '06 #18
am****@gmail.com wrote:
According to Wolfgang Bangerth in the link I posted: "It is part of the
Technical Corrigendum TC1 of the standard, however, and therefore
(retroactively) considered part of C++98"
That isn't actually true - it isn't part of TC1. Rather, it has been
voted into the working paper for the next standard, which means it will
be in the next standard. See here:
http://www.open-std.org/jtc1/sc22/wg...efects.html#45
I am curious to know that if some thing goes into a TC is it equivalent
to being the standard or is it still in the 'proposed' status.


It is equivalent to being in the standard. TCs are for corrections to
defects in the standard, and in effect modify the original standard when
they are released. However, this issue didn't get solved for TC1,
presumably because it wasn't actually a defect in the original standard,
but rather an improvement to the standard and thus a candidate for the
next standard (called C++0x colloquially).

Tom
May 5 '06 #19

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

Similar topics

7
1794
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...
3
21370
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...
7
9741
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...
5
1838
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
2648
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 )...
22
23317
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...
3
9811
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...
5
1899
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
1747
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...
0
7223
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
7115
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
7377
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...
0
7489
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...
0
5624
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,...
1
5047
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...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1547
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 ...
1
762
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.