Final Class in c++
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: Final Class in c++

Hybrid View

  1. #1
    Join Date
    Jul 2007
    Posts
    227

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

  2. #2
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,725

    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

  3. #3
    Join Date
    Nov 2012
    Location
    Kolkata, WestBengal, India
    Posts
    15

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

  4. #4
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,725

    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

  5. #5
    Join Date
    Jul 2007
    Posts
    227

    Re: Final Class in c++

    thanx for answers

  6. #6
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,925

    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.

  7. #7
    Join Date
    Jul 2007
    Posts
    227

    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

  8. #8
    Join Date
    May 2009
    Posts
    2,413

    Re: Final Class in c++

    Isn't there a keyword "final" in C++ 11 that can be used to prevent further inheriting?

  9. #9
    Join Date
    Jul 2007
    Posts
    227

    Re: Final Class in c++

    There is not keyword like "final" is present in c++

  10. #10
    Join Date
    May 2009
    Posts
    2,413

    Re: Final Class in c++

    Quote Originally Posted by Rajesh1978 View Post
    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 05: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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center