#include <iostream>

using namespace std;

class Lock
{
	friend class First;
	private:
		Lock()
		{
		}
};

class First : virtual public Lock
{
	public:
		void function()
		{
			cout << "function1" << endl;
		}
};

class Second: public First
{
	public:
		void function2()
		{
			cout << "function2" << endl;
		}
};

int main()
{
	First f;
	f.function();

	Second s;
	s.function2();
}

Error:
classVirtDer.cpp(39) : error C2248: 'Lock::Lock' : cannot access private member declared in class 'Lock'
classVirtDer.cpp(9) : see declaration of 'Lock::Lock'
classVirtDer.cpp(6) : see declaration of 'Lock'
This diagnostic occurred in the compiler generated function 'Second::Second'

But when i remove the class Second, it works fine. Can someone explain?

Friendship isn't inherited.

You declared a private constructor for the Lock class, in which it is only accessible either within that class or within a scope that has the right to access that Constructor.

Also because you've defined a constructor there is no default/dummy constructor provided.

Because First is a friend of Lock, First has the right to access Lock's private constructor.

Second does not inherit friendship of Lock despite the fact that Second derives from First.

In order to make the code work, you'll have to make Lock declare Second as a friend also--

#include <iostream>

using namespace std;

class Lock
{
	friend class First;
	friend class Second;
	private:
		Lock()
		{
		}
};

class First : virtual public Lock
{
	public:
		void function()
		{
			cout << "function1" << endl;
		}
};

class Second: public First
{
	public:
		void function2()
		{
			cout << "function2" << endl;
		}
};

int main()
{
	First f;
	f.function();

	Second s;
	s.function2();
	cin.get();
       return 0;
}

i think its more because of the keyword 'virtual', just try and remove that and it compiles and excutes fine. Normally the class Second wil call the ctor of class First and then class First will call the ctor of Lock so i dont need to make 'Second' a friend of 'Lock'. but when i derive from the class 'Lock' virtually this doesnt happen.

commented: Yeah I completely forgot that it's the First classes responsibility to call the private Constructor, not the Second +1

I think I figured out the problem.

The link http://nxgenesis.blogspot.com/2007/11/rules-virtual-base-class-and-side.html states that Virtual Base classes are constructed before any Non-virtual Base class.

"virtual base classes and inheritance offer a set of rules which every c++ programmar should be aware of. Specifically:
virtual base classes are constructed before all non-virtual base classes.
virtual base class's constructor is directly invoked by the most-derived class's constructor."

When Second is instantiated, before the First constructor can run the Lock constructor runs before the First and because of that it's as if Second is trying to directly access Lock's private method since Second is the most-derived class and attempts to directly call the Lock constructor.

As stated previously, Friendship isn't inherited so even though First is a friend of Lock and Second derives from First, Second isn't a friend of Lock.

I'm fairly certain that this is the reason.

Change the private constructor into a public constructor..

.....???

thanks Edwards, that makes sense. Need to understand the virtual base classes more.

>Change the private constructor into a public constructor..

yes but then we wouldn't have faced the problem and wouldn't have come to know all this. it was just something i was trying to do.

dont use the public constructor use private constructor in place of public constructor

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.