943,929 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 2688
  • C++ RSS
Jul 9th, 2008
0

Virtual Inheritance Question

Expand Post »
c++ Syntax (Toggle Plain Text)
  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?
Similar Threads
Featured Poster
Reputation Points: 431
Solved Threads: 116
Practically a Master Poster
Agni is offline Offline
654 posts
since Dec 2007
Jul 9th, 2008
0

Re: Virtual Inheritance Question

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

c++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 392
Solved Threads: 108
Posting Shark
Alex Edwards is offline Offline
971 posts
since Jun 2008
Jul 9th, 2008
1

Re: Virtual Inheritance Question

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.
Featured Poster
Reputation Points: 431
Solved Threads: 116
Practically a Master Poster
Agni is offline Offline
654 posts
since Dec 2007
Jul 9th, 2008
0

Re: Virtual Inheritance Question

Reputation Points: 12
Solved Threads: 7
Light Poster
RenjithVR is offline Offline
41 posts
since Mar 2008
Jul 9th, 2008
0

Re: Virtual Inheritance Question

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.
Reputation Points: 392
Solved Threads: 108
Posting Shark
Alex Edwards is offline Offline
971 posts
since Jun 2008
Jul 9th, 2008
0

Re: Virtual Inheritance Question

Change the private constructor into a public constructor..

.....???
Last edited by cikara21; Jul 9th, 2008 at 5:06 am.
Reputation Points: 47
Solved Threads: 69
Posting Whiz
cikara21 is offline Offline
340 posts
since Jul 2008
Jul 9th, 2008
0

Re: Virtual Inheritance Question

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.
Featured Poster
Reputation Points: 431
Solved Threads: 116
Practically a Master Poster
Agni is offline Offline
654 posts
since Dec 2007
Jul 9th, 2008
0

Re: Virtual Inheritance Question

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
Reputation Points: 10
Solved Threads: 1
Newbie Poster
ash05 is offline Offline
6 posts
since Jul 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Convert to ANSI
Next Thread in C++ Forum Timeline: pointers to function problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC