Topic: friends and inheritance
Author: Yogish Baliga <baliga@synopsys.com>
Date: 1999/05/10 Raw View
Derived class cannot access the private members of the base class,
but can access the protected and public members of the base class.
If you don't specify the storage type of members, it is taken as private by
default in class, but is taken as public in struct.
Hope this answers your question.
-- Baliga
Ahn Ki-yung wrote:
[Moderator note: quotation sniped - please don't overquote --vb]
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Ahn Ki-yung <kyagrd@chiak.kaist.ac.kr>
Date: 1999/05/07 Raw View
When you compile the code below,
( VC++ 6.0, gcc 2.8.1, egcs ??? all the same )
================================
class F
{
int ff;
};
class S : public F
{
friend void fts(S& tmp);
};
void fts(S& tmp)
{
tmp.ff = 1;
}
int main()
{
S tmp;
fts(tmp);
}
================================
error 'ff' in line(?) : cannot access private member declared in class 'F'
================================
there comes an error like above.
I think this very weird. Logically friend of a derived class should be friend of its
super class. Don't you think so. This is really bothersome when you want to
make a helper fucntion or overload the << operator of a derived class.
See the case below. The helper fuction print() won't work at all !!
======================================
class People
{
private:
string name;
// ... ...
};
class Student : public People
{
private:
int student_No;
// ... ...
friend void print(const Student&)
};
void print(const Student& s)
{
cout<<s.name <<s.student_No;
// error : cannot access private member People::name
}
======================================
Same problem will occur when you want to overlaod the << operator
of the derived class.
As you See I could never get the idea why the friend of derived class
must not be friend of the super class. Why did they make the language
like this ? Is it because of diffculty of implementing compilers, or is there
any other conceptual reason ?
How did you do get out when you were stuck in the problem like this ?
Have any good suggestions to solve this kind of problem ?
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Valentin Bonnard <Bonnard.V@wanadoo.fr>
Date: 1999/05/07 Raw View
Ahn Ki-yung wrote:
>
> When you compile the code below,
> ( VC++ 6.0, gcc 2.8.1, egcs ??? all the same )
And all right.
> class F
> {
> int ff;
> };
>
> class S : public F
> {
> friend void fts(S& tmp);
> };
>
> void fts(S& tmp)
> {
> tmp.ff = 1;
> }
> ================================
> error 'ff' in line(?) : cannot access private member declared in class 'F'
> ================================
> there comes an error like above.
>
> I think this very weird.
This is just common sens actually. The derived class has no
access to the private members of its base class. Saying that
another function is a friend doesn't give you access to
something you don't have in the first place.
> Logically friend of a derived class should be friend of its
> super class. Don't you think so.
OK, put it this way: I grant a sister the right to use my
bank account. Then my sister should have access to my employer's
bank account. Don't you think so ?
If you think about it, you'll realize that what you suggest
is completly non-sensical.
--
Valentin Bonnard
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Ron Natalie <ron@sensor.com>
Date: 1999/05/07 Raw View
Ahn Ki-yung wrote:
>
>
> I think this very weird. Logically friend of a derived class should be friend of its
> super class. Don't you think so.
No I don't. But I don't think friendship is your problem. ff
isn't accessible to members of class S, let alone friends.
You need to use the "protected" access on ff, by default it
is private.
If you chage F to read:
class F {
protected:
int ff;
};
Your program will compile just fine.
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Marc Girod <girod@stybba.ntc.nokia.com>
Date: 1999/05/07 Raw View
>>>>> "Ahn" == Ahn Ki-yung <kyagrd@chiak.kaist.ac.kr> writes:
Ahn> I think this very weird. Logically friend of a derived class
Ahn> should be friend of its super class.
S can only grant friendship to something it can access itself. ff is
private in F, so S cannot access it, nothing strange there.
If you want that S accesses F data, you have to make it protected.
Well, I don't recommend that, in fact.
--
Marc Girod Hiomo 5/1 Voice: +358-9-511 23746
Nokia Telecommunications P.O. Box 320 Mobile: +358-40-569 7954
NWS/NMS/NMS for Data 00045 NOKIA Group Fax: +358-9-511 23580
Finland marc.girod@ntc.nokia.com
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Jim Hyslop <jim.hyslop@leitch.com>
Date: 1999/05/07 Raw View
In article <3731EAB3.219668E5@daidun.kaist.ac.kr>,
Ahn Ki-yung <kyagrd@chiak.kaist.ac.kr> wrote:
[snip]
> I think this very weird. Logically friend of a derived class should be friend
of its
> super class. Don't you think so.
Not at all. Friendship among C++ classes and children quite often mirrors
real-life friendships among people - are your friends also your parents'
friends? Not likely.
[snip]
> As you See I could never get the idea why the friend of derived class
> must not be friend of the super class. Why did they make the language
> like this ? Is it because of diffculty of implementing compilers, or is there
> any other conceptual reason ?
Think about this for a moment - if the language allowed what you suggested,
then there would be absolutely no such thing as encapsulation.
Consider a class that you've just written, that's properly encapsulated:
class yours
{
int currentValue; // Must be in the range 1-100.
// Let's be melodramatic and say that this represents a critical
// range in a nuclear reactor control system.
public:
// access methods.
};
Now, say that I'm a devious hacker who wants to change the value of
currentValue to anything I wanted to. So, all I'd do is to derive a child
class, and declare a friend:
class mine : public yours
{
friend void rapePillageAndPlunderObject(mine &);
};
void rapePillageAndPlunderObject(mine &victim)
{
victim.currentValue = 2000; // Boom, the nuclear reactor just melted down.
}
--
Jim
I ignore all email from recruitment agencies.
Please do not send me email with questions - post here.
-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]