I'm new to these concepts but a derived class cannot access private members of the base class because they are simply not accessible externally. But what if I define the derived class in a friend function to the base class. will the private members of the base class be successfully derived and if yes, will they be as public or private, or depend on the type of derivation?

Thanks,
Abhinav

Dani AI

Generated

Short answer: a class only gets access to a base class’s private members if the base explicitly names it as a friend. ’s VC++ example works because the base declared the derived class as a friend; that makes the derived class’s members allowed to refer to the base’s private data. The friend relation is an explicit permission, not something “inherited” automatically. (Friendship is neither transitive nor inherited.) [cppreference: friend].(https://en.cppreference.com/w/cpp/language/friend)

Clarification of common confusions: private members remain the private implementation of the declaring class — they don’t become public or protected in the derived class just because the derived class can access them as a friend. Access control is checked against the class that declared the member: only that class’s members and its friends may access its private members. The kind of derivation (public/protected/private) affects visibility of base subobjects to outsiders, but it does not change the access-specifier of the original private members. [cppreference: access control].(https://en.cppreference.com/w/cpp/language/access)

About defining a derived class inside a friend function (what asked): language rules for local classes and friend declarations are subtle and have changed and been clarified over time. A local class lives in function scope and name-lookup/access rules differ from ordinary namespace-scope classes; compiler behavior has varied historically. Relying on implicit “friend” behavior for a class defined only inside a friend function is brittle and non‑portable. See the rules for local classes and friend declarations for the precise constraints. [cppreference: class (local classes)].(https://en.cppreference.com/w/cpp/language/class) [cppreference: friend (notes)].(https://en.cppreference.com/w/cpp/language/friend)

Practical advice: if you need guaranteed, portable access make the intent explicit — either declare the (non‑local) derived class as a friend in the base (friend class Derived;), or provide a protected accessor/controlled interface. Avoid relying on compiler quirks; test on your target compilers.

Recommended Answers

All 6 Replies

does C++ even have method-local inner classes?

This is compiled and run with VC++ 2005 Express with no errors.

class A;
class Base
{
private:
	int m_x;
public:
	friend A;
	Base() {m_x = 1;}
};

class A : public Base
{
public:
	A() {m_x = 2;}
	int getX() {return m_x;}
};

int main()
{
	A a;
	cout << a.getX() << endl;
	return 0;
}

But what if I define the derived class in a friend function to the base class. will the private members of the base class be successfully derived and if yes, will they be as public or private, or depend on the type of derivation?

Yes, they'll be successfully derived. That's going to happen no matter where the derived class is defined. It's a basic rule of inheritance. ;) The funny thing is that the derived class sort of inherits the access rights of the friend function and the private members of the base class are accessible to both the friend function and the derived class thanks to the "is a" rule.

I'm not sure that's right, but it compiles on the strictest modes of the most conforming compiler I know of. :)

Thanks, this is a great first experience here on Daniweb. The replies were very informative.


The funny thing is that the derived class sort of inherits the access rights of the friend function and the private members of the base class are accessible to both the friend function and the derived class thanks to the "is a" rule.

So, you mean to say that the friend function is also now a friend function to the derive class. That practically means that within that function, any private members of the derived class can be accessed, right?

So, you mean to say that the friend function is also now a friend function to the derive class. That practically means that within that function, any private members of the derived class can be accessed, right?

That's the impression that I get. :) I'm not sure you can rely on it because the rules for friends shouldn't do that. A friend of mine is not necessarily a friend of my children. ;) But I don't claim to understand most of C++, and there could be some tricky rules hidden in there that allow your situation. A good place to ask the super gurus is comp.lang.c++.

Ok, thanks. I'll check out the group you mentioned too.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.