Virtual Inheritance Question

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Dec 2007
Posts: 449
Reputation: Agni is a jewel in the rough Agni is a jewel in the rough Agni is a jewel in the rough 
Solved Threads: 70
Sponsor
Agni's Avatar
Agni Agni is offline Offline
Posting Pro in Training

Virtual Inheritance Question

 
0
  #1
Jul 9th, 2008
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class Lock
  6. {
  7. friend class First;
  8. private:
  9. Lock()
  10. {
  11. }
  12. };
  13.  
  14. class First : virtual public Lock
  15. {
  16. public:
  17. void function()
  18. {
  19. cout << "function1" << endl;
  20. }
  21. };
  22.  
  23. class Second: public First
  24. {
  25. public:
  26. void function2()
  27. {
  28. cout << "function2" << endl;
  29. }
  30. };
  31.  
  32. int main()
  33. {
  34. First f;
  35. f.function();
  36.  
  37. Second s;
  38. s.function2();
  39. }

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:econd'

But when i remove the class Second, it works fine. Can someone explain?
thanks
-chandra
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: Virtual Inheritance Question

 
0
  #2
Jul 9th, 2008
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--

  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class Lock
  6. {
  7. friend class First;
  8. friend class Second;
  9. private:
  10. Lock()
  11. {
  12. }
  13. };
  14.  
  15. class First : virtual public Lock
  16. {
  17. public:
  18. void function()
  19. {
  20. cout << "function1" << endl;
  21. }
  22. };
  23.  
  24. class Second: public First
  25. {
  26. public:
  27. void function2()
  28. {
  29. cout << "function2" << endl;
  30. }
  31. };
  32.  
  33. int main()
  34. {
  35. First f;
  36. f.function();
  37.  
  38. Second s;
  39. s.function2();
  40. cin.get();
  41. return 0;
  42. }
Last edited by Alex Edwards; Jul 9th, 2008 at 2:57 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 449
Reputation: Agni is a jewel in the rough Agni is a jewel in the rough Agni is a jewel in the rough 
Solved Threads: 70
Sponsor
Agni's Avatar
Agni Agni is offline Offline
Posting Pro in Training

Re: Virtual Inheritance Question

 
1
  #3
Jul 9th, 2008
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.
thanks
-chandra
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 41
Reputation: RenjithVR is an unknown quantity at this point 
Solved Threads: 7
RenjithVR RenjithVR is offline Offline
Light Poster

Re: Virtual Inheritance Question

 
0
  #4
Jul 9th, 2008
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: Virtual Inheritance Question

 
0
  #5
Jul 9th, 2008
I think I figured out the problem.

The link http://nxgenesis.blogspot.com/2007/1...-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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 338
Reputation: cikara21 is an unknown quantity at this point 
Solved Threads: 64
cikara21's Avatar
cikara21 cikara21 is offline Offline
Posting Whiz

Re: Virtual Inheritance Question

 
0
  #6
Jul 9th, 2008
Change the private constructor into a public constructor..

.....???
Last edited by cikara21; Jul 9th, 2008 at 5:06 am.
.:-cikara21-:.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 449
Reputation: Agni is a jewel in the rough Agni is a jewel in the rough Agni is a jewel in the rough 
Solved Threads: 70
Sponsor
Agni's Avatar
Agni Agni is offline Offline
Posting Pro in Training

Re: Virtual Inheritance Question

 
0
  #7
Jul 9th, 2008
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.
thanks
-chandra
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 6
Reputation: ash05 is an unknown quantity at this point 
Solved Threads: 1
ash05 ash05 is offline Offline
Newbie Poster

Re: Virtual Inheritance Question

 
0
  #8
Jul 9th, 2008
dont use the public constructor use private constructor in place of public constructor
Last edited by Narue; Jul 9th, 2008 at 10:15 am. Reason: Removed fake signature
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC