954,152 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Virtual Inheritance Question

#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?

Agni
Practically a Master Poster
655 posts since Dec 2007
Reputation Points: 431
Solved Threads: 116
 

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;
}
Alex Edwards
Posting Shark
972 posts since Jun 2008
Reputation Points: 392
Solved Threads: 109
 

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.

Agni
Practically a Master Poster
655 posts since Dec 2007
Reputation Points: 431
Solved Threads: 116
 
RenjithVR
Light Poster
41 posts since Mar 2008
Reputation Points: 12
Solved Threads: 7
 

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.

Alex Edwards
Posting Shark
972 posts since Jun 2008
Reputation Points: 392
Solved Threads: 109
 

Change the private constructor into a public constructor..

.....???

cikara21
Posting Whiz
340 posts since Jul 2008
Reputation Points: 47
Solved Threads: 69
 

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.

Agni
Practically a Master Poster
655 posts since Dec 2007
Reputation Points: 431
Solved Threads: 116
 

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

ash05
Newbie Poster
6 posts since Jul 2008
Reputation Points: 10
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You