-
November 21st, 2012, 03:43 AM
#1
Final Class in c++
i got this program is in http://www.parashift.com/c++-faq-lit...l-classes.html
the code is
Code:
#include <iostream>
#include <memory>
using namespace std;
class Fred;
class FredBase {
private:
friend class Fred;
FredBase() {cout<<"FredBase Cons"<<endl; }
};
class Fred : virtual private FredBase {
public:
Fred()
{
cout<<"Fred Cons"<<endl;
}
};
class Leaf : public Fred
{
};
int main()
{
Leaf f;
return 0;
}
It works and do not allow to instantiate the Leaf object.
I have confusion about the virtual keyword in (class Fred : virtual private FredBase )
why it is required.
If i remove the virtual keyword it is allowing me to create the Leaf f object.
What is the importance of virtual here ?
-
November 21st, 2012, 06:08 AM
#2
Re: Final Class in c++
The key line is in the article.
"Class Fred can access FredBase's ctor, since Fred is a friend of FredBase, but no class derived from Fred can access FredBase's ctor, and therefore no one can create a concrete class derived from Fred. "
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
-
November 21st, 2012, 06:25 AM
#3
Re: Final Class in c++
That's really a good way to do - "How can I set up my class so it won't be inherited from?"
Well, I also never tried in this way, but what I suspect is that -
- because of the virtual private FredBase, Fred will be able to access FredBase(), but due to it's Private nature, no further class will be able to create instance using Fred().
- now, if we remove virtual keyword, (I suppose) Fred will take the Default Cons of FredBase, rather than which is already defined under Private: - hence, eventually Leaf will be able to create an instance.
So, importance of virtual here is, not to allow Fred to use object overloading concept here.
May be somebody else can clear the idea on the same.
Raju2001006
Please put [CODE][/CODE] tags around your code to preserve indentation and make it more readable...
-
November 21st, 2012, 07:08 AM
#4
Re: Final Class in c++
It's to do with this bit.
"the most derived class's ctor needs to directly call the virtual base class's ctor"
Only 'Fred' can do this as it is a 'friend'. It is private to all other derived classes.
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
-
November 21st, 2012, 10:40 AM
#5
-
November 21st, 2012, 10:44 AM
#6
Re: Final Class in c++
It is an "exploit" of how virtual inheritance needs to work. This isn't really what virtual inheritance is intended for, hence it's exploiting the way it works to achieve a particular behaviour.
Without the virtual inheritance:
Only Fred can derive from Fredbase because Fredbase has a private constructor, and only Fred is a friend.
But nothing prevents you from deriving further from Fred.
With virtual inheritance, the order in which the base classes are constructed (and who's responsible for doing this) changes.
-
November 21st, 2012, 12:42 PM
#7
Re: Final Class in c++
JohnW@Wessex
It's to do with this bit.
"the most derived class's ctor needs to directly call the virtual base class's ctor"
One more doubt. why the most derived class calls the virtual Base class directly
-
November 22nd, 2012, 03:23 AM
#8
Re: Final Class in c++
Isn't there a keyword "final" in C++ 11 that can be used to prevent further inheriting?
-
November 22nd, 2012, 04:58 AM
#9
Re: Final Class in c++
There is not keyword like "final" is present in c++
-
November 22nd, 2012, 05:34 AM
#10
Re: Final Class in c++
Originally Posted by Rajesh1978
There is not keyword like "final" is present in c++
Well, in C++ 11 there's something called virt-specifiers. They're override and final.
I've only noticed this in passing but from what I understand final can be used to declare a virtual function non-overridable (and override would mean it must be overridden). It's specified in 10.3 Virtual functions.
There's also a class-virt-specifier called final. It can be used to render a whole class non-inheritable. It's specified in 9 Classes.
Last edited by nuzzle; November 22nd, 2012 at 06:02 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|