Thread: How to prevent a function from being overriden?

  1. #16
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by 7stud
    Your code won't compile for me. If I try to create a ProtectedBase object, I get linker errors(VC6).
    Correction: the example compiles with VC6. It does not LINK.

    That's because the example only included the declaration of Base. It did not include implementation of Base's member functions.
    Quote Originally Posted by 7stud
    And I don't see how it can prevent a class derived from ProtectedBase from defining functions named SomeMethod() and SomeOtherMethod().
    I didn't claim it would do that. It is impossible to prevent any class from defining member functions of any name (unless that name is a keyword).

    What I provided is a *partial* solution to the problem, in that it prevents some behaviours normally associated with virtual functions in classes derived from ProtectedBase.

    ProtectedBase provides a member named SomeMethod, which cannot be overridden as a virtual function, despite the fact it calls Base's virtual function named SomeMethod(). While it is not possible to prevent a class derived from ProtectedBase defining a member named SomeMethod(), it does prevent a version of SomeMethod() in that derived class being called as a virtual function.

    The SomeOtherMethod() in ProtectedBase remains virtual and can therefore be overridden.

    Try this usage;
    Code:
    class Something : public ProtectedBase
    {
          public:
    
                void SomeMethod() {};
                void SomeOtherMethod() {};
    };
    
    int main()
    {
          ProtectedBase *object = new Something;   // assume we have a public default constructor and suitable virtual destructors
    
          object->SomeMethod();   // will invoke ProtectedBase::SomeMethod() which will invoke Base::SomeMethod(), NOT Something::SomeMethod();
    
          object->SomeOtherMethod();  // will invoke Something::SomeOtherMethod()
    
    }

  2. #17
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    It is impossible to prevent any class from defining member functions of any name
    Well, that's what the op wants to do, and that's what a final function does in Java. The op wants the base class function to be inherited in the derived class, but he doesn't want the derived class to be able to hide or override the inherited function.
    Last edited by 7stud; 11-12-2005 at 06:19 AM.

  3. #18
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by 7stud
    Well, that's what the op wants to do, and that's what a final function does in Java. The op wants the base class function to be inherited in the derived class, but he doesn't want the derived class to be able to hide or override the inherited function.
    ... which means that, in C++, the best he can get will be a set of partial solutions .... such as those in my previous posts.

  4. #19
    On the run thomas_nibu's Avatar
    Join Date
    Nov 2005
    Location
    Trivandrum, Kerala, India
    Posts
    7

    Thumbs up

    Hey thank a lot guys.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM