| | |
Virtual Inheritance Question
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
c++ Syntax (Toggle Plain Text)
#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:
econd'But when i remove the class Second, it works fine. Can someone explain?
thanks
-chandra
-chandra
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--
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)
#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; }
Last edited by Alex Edwards; Jul 9th, 2008 at 2:57 am.
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
-chandra
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.
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.
![]() |
Similar Threads
- problem in database creation (C++)
- casting from userData (C++)
- 'this' in initializer list - bad, or irrelevant? (C++)
- I have another homework question that i'm not sure what the answer is. (C)
- virtual methods and inheritance (C++)
- Need example of how to use INHERITANCE! (C++)
- Why Data Structures???...QUESTIONS INSIDE (C++)
Other Threads in the C++ Forum
- Previous Thread: Convert to ANSI
- Next Thread: pointers to function problem
Views: 1611 | Replies: 7
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays assignment based beginner binary c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multidimensional multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets





