473,503 Members | 5,004 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Derived class friend of base class

3 New Member
Hi all,

Why in the following example can the derived class not see the base class:

class base
{
private:
int a;

friend class derived;
}

class derived : public base
{
public:
derived() : a(0) { ; } //error a is private
}

why doesn't the friend declaration allow access to a? is it because it is not inherited in the derived class?
Sep 13 '06 #1
3 7376
Banfa
9,065 Recognized Expert Moderator Expert
You don't have to declare a derived class as a friend of it's base class. It is automatically a friend of it's base class.

Being a friend means that you can access the protected members of the class. The private members are always only accessable by the class it self.
Sep 13 '06 #2
davidceo
3 New Member
this is actually legal c++ code. my problem was that I linking against an outdated library!

the derived class inherits the base class and is a friend of the base class so can access its member variables. (but it doesn't inherit the friend itself, which in this case would make it a friend of itself!)
Sep 13 '06 #3
davidceo
3 New Member
sorry i made a mistake. This is legal code:

class base
{
private:
int a;

friend class derived;
}

class derived : public base
{
public:
derived() { a=0; }
}

the error i had was because i was trying to initialise a member of a base class ('a') in a derived classes initialisation list (which as we all know is wrong).
Sep 13 '06 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

12
2543
by: Tim Clacy | last post by:
Your expertise will be appreciated... Here's a general templatised class; all specialisations of this class should have a pointer to a specialisation of the same, templatised type: ...
9
4960
by: Banaticus Bart | last post by:
I wrote an abstract base class from which I've derived a few other classes. I'd like to create a base class array where each element is an instance of a derived object. I can create a base class...
3
2237
by: Zycor | last post by:
Anybody have good knowledge of delete that can answer this simple question? I have a base class that contains pointers to its own type, those pointers will contain derived classes (acting...
1
1209
by: user | last post by:
Hi, If I assign in the base class' constructor: myMember = 5; and assign in the derived class' constructor: myMember = 10; where myMember was derived from the base class,
4
1820
by: Nataraj M | last post by:
Hi, I just don't want anybody derive a class from my class. For example: /////////////////////// //MY CODE class MyClass { .... }; ///////////////////////
8
3050
by: TS | last post by:
I am trying to get set a property of a control on the inherited class from base class. I imagine i have to use reflection, so could someone give me the code to do it? something like this?...
2
4555
by: Stephan Hoffmann | last post by:
Hi, I'm new to std::auto_ptr<> and wanted to use it with a base class and several derived classes. I'm using gcc 3.3.5 and get a compile error I don't know how to resolve when compiling the...
15
7338
by: Anthony Greene | last post by:
This is probably a very introductory object-oriented question, but it has been nagging me for years, and since I've never been able to find the right answer, I've had to work around it with...
9
1939
by: fgh.vbn.rty | last post by:
Say I have a base class B and four derived classes d1, d2, d3, d4. I have three functions fx, fy, fz such that: fx should only be called by d1, d2 fy should only be called by d2, d3 fz should...
0
1250
by: ma740988 | last post by:
Consider # include <iostream> # include <vector> # include <typeinfo> # include <string> class BaseMsg { friend std::ostream& operator<<( std::ostream&, const BaseMsg& ); std::string name...
0
7063
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
7258
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
7313
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
7441
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...
1
4987
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
4663
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
3156
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...
0
1489
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 ...
0
366
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...

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.