How to prevent a function from being overriden?

This is a discussion on How to prevent a function from being overriden? within the C++ Programming forums, part of the General Programming Boards category; Hi guys, Can any one of you suggest a way to prevent a function from being overriden in a derived ...

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

    Smile How to prevent a function from being overriden?

    Hi guys,

    Can any one of you suggest a way to prevent a function from being overriden in a derived class.

    I want the derived class to use the function too. But want to prevent it from overriding it.

    Java has a keyword called in final. Anything in c++?

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    85
    I'd just make the base class function virtual, and just make a call to the base classes function in the derived class.

  3. #3
    On the run thomas_nibu's Avatar
    Join Date
    Nov 2005
    Location
    Trivandrum, Kerala, India
    Posts
    7
    Quote Originally Posted by jlf029
    I'd just make the base class function virtual, and just make a call to the base classes function in the derived class.
    I know I can do it like this. But the problem is it can be overriden. And the behavior can be changed. I don't want this to happen.

    The final keyword in java is a good example. It prevents the function from being overriden.

  4. #4
    Bond sunnypalsingh's Avatar
    Join Date
    Oct 2005
    Posts
    162
    do this

    derived class object.class name::function name

    or

    classname::function in any other member function

  5. #5
    On the run thomas_nibu's Avatar
    Join Date
    Nov 2005
    Location
    Trivandrum, Kerala, India
    Posts
    7
    didn't get you. Sorry.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,673
    I thought that the final keyword in Java completely prevents derivation for the class, rather than allowing derivation while preventing overloading of particular virtual functions.

    That can be done as follows;
    Code:
    class BaseLocker
    {
        private:
    
           friend class Base;
           BaseLocker() {};
           BaseLocker(const BaseLocker &) {};
           ~BaseLocker();
    };
    
    class Base: virtual public BaseLocker
    {
       public:
           Base();
    };
    
    class Derived: public Base
    {
    
    };
    
    Derived derived;    // error as BaseLocker must be constructed first, and it's constructors are private

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

    But this is not what I meant.

    --in java--
    Code:
    public final void func()
    {}
    This will prevent the function from being overriden. But it can be used.

    I want an equivalent of this in C++.

    The above code applies to the whole class. But I want it only for certain functions. I hope there should be another way.

    Thanks,
    Nibu thomas.

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    I thought that the final keyword in Java completely prevents derivation for the class, rather than allowing derivation while preventing overloading of particular virtual functions.
    The final keyword in Java can be used to qualify classes, functions, and member variables. final has that effect if you put it in front of a class name. If you put it in front of a function name, the compiler will give you an error if you try to override it in a derived class.
    Last edited by 7stud; 11-09-2005 at 06:40 AM.

  9. #9
    On the run thomas_nibu's Avatar
    Join Date
    Nov 2005
    Location
    Trivandrum, Kerala, India
    Posts
    7
    Yeah this is what I want. I am assuming that a function with the same signature does not exist in the super class. Then no problem.

    I want to simulate this is C++

  10. #10
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    I don't think you can prevent a name from being used in C++.

  11. #11
    Registered User
    Join Date
    Dec 2004
    Location
    UK
    Posts
    109
    If you don't declare the function virtual it will be statically bound, which means that as long as you access the derived objects through a base class pointer (or reference) the base class member will be used even if it being overridden in the derived class.

  12. #12
    On the run thomas_nibu's Avatar
    Join Date
    Nov 2005
    Location
    Trivandrum, Kerala, India
    Posts
    7
    Quote Originally Posted by sigfriedmcwild
    If you don't declare the function virtual it will be statically bound, which means that as long as you access the derived objects through a base class pointer (or reference) the base class member will be used even if it being overridden in the derived class.
    Yeah Thanks,

    I know about this. But I wanted to stop this stuff from happening.

  13. #13
    Registered User
    Join Date
    Jun 2005
    Posts
    6,673
    AFAIK, there is no mechanism in C++ to take an existing virtual function in a class, and make it non-virtual as far as derived classes are concerned.

    It might be possible to use delegation to do something like this;
    Code:
    class Base
    {
         public:
             // other methods
            virtual void SomeMethod();
    
            virtual void SomeOtherMethod();
    };
    
    class ProtectBase
    {
         public:
            
              void SomeMethod() {base.SomeMethod();};
    
              virtual void SomeOtherMethod() {base.SomeOtherMethod();};
         private:
    
                 Base base;
    };
    If you derive your classes from ProtectBase, SomeMethod() is no longer a virtual function, but SomeOtherMethod() is.

    The problem with this approach is that you need to prevent other classes from being derived from Base. My previous post in this thread gives some indication of how to do that....

  14. #14
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Your code won't compile for me. If I try to create a ProtectedBase object, I get linker errors(VC6). And I don't see how it can prevent a class derived from ProtectedBase from defining functions named SomeMethod() and SomeOtherMethod().
    Last edited by 7stud; 11-12-2005 at 03:26 AM.

  15. #15
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Well there are a couple problems with what you want to do.

    First there is no way in c++ to turn a virtual function into a non virtual function.
    Second derived classes can legally redefine non virtual functions. You should not but its still possible.

    But lets assume that the programmer won't violate non virtual functions. Then all you have to do is some private inheritance or compisition. Now the problem with this method is that you'd have to redo the interface and if part of the interface was added afterwards to any of the base classes you'd have to go through and add it to your class.

    Code:
    #include <iostream>
    using namespace std;
    
    class Base
    {
            int x;
            public:
            Base() : x(0) {}
            virtual void Foo()
            {
                    cout<<x<<' ';
            }
    };
    
    class Derv1 : Base
    {
            int x;
            public:
            Derv1() : x(1) {}
            virtual void Foo()
            {
                    cout<<x<<' ';
                    Base::Foo();
            }
    };
    
    class GoBetween
    {
            Derv1 derv;
            int x;
            public:
            GoBetween() : derv(), x(2) {}
            void Foo()
            {
                    cout<<x<<' ';
                    derv.Foo();
            }
    };
    
    int main()
    {
            GoBetween gob;
            gob.Foo();
            cout<<endl;
    }

Page 1 of 2 12 LastLast
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, 07:54 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 12:45 PM
  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, 07:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21