468,496 Members | 1,818 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,496 developers. It's quick & easy.

Derived class friend of base class

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 6273
Banfa
9,055 Expert Mod 8TB
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
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
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

Post your reply

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

Similar topics

4 posts views Thread by Nataraj M | last post: by
reply views Thread by NPC403 | last post: by
3 posts views Thread by gieforce | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.