Is there any thing equivalent to NotOverridable in C++?

Hi guys,

My problem is this.

class Base
{
   public:
      void  OneFunction()
      {
          cout<<"Base";  
      }
};


class Derived:public Base
{
   public:      
      //overriden, I don't want this to happen.
      void OneFunction()
      {
         cout<<"Base class function gone";
      }
};

Now base class implementation of OneFunction is gone. The derived class has overriden (even though not really) the function. Whenever derived calls OneFunction it will be calling it's own implementation. I want to stop this from happening.

Is it possible

I am actually writing classes which are meant to be distributed. Problems are occurring due to the above mentioned reasons.

Help Needed.

Thanks,
Nibu thomas.


Answer this question

Is there any thing equivalent to NotOverridable in C++?

  • Dwight6531

    class Base
    {
       public:
          void  OneFunction() sealed
          {
              cout<<"Base";  
          }
    };

    David Anton
    www.tangiblesoftwaresolutions.com
    Instant C#: VB.NET to C# Converter
    Instant VB: C# to VB.NET Converter
    Instant C++: C# to C++ Converter
    Instant J#: VB.NET to J# Converter
    Clear VB: Cleans up VB.NET code

  • petedee

  • kayers

    Do you own all the code If that is the case you can write it like below. However if you are having others use these classes you should assume that they are writing the behaviour they need and if it means to override a function in their derived class all calls to that function will not go to your base class.



    class Base
    {
       public:
          void  OneFunction()
          {
              cout<<"Base"; 
          }
    };


    class Derived:public Base
    {
       public:     
          //overriden, I don't want this to happen.
          void OneFunction()
          {
             cout<<"Base class function gone";
          }

        void CallOneFunction()
        {
           // Calls OneFunction() in base class instead of overriden
           Base::OneFunction();
        }
    };

     


    With a static function you can never override it.



    class Base
    {
       public:
          statiuc void OneFunction()
          {
              cout<<"Base"; 
          }
    };

    //Always called like this
    Base::OneFunction();

     



  • Ramesh Rajagopal

    Can you suggest a workaround. If there are any.Sad

    Thanks
    Nibu thomas.

  • Noah Falk - MSFT

    Yeah can you please explain with some code.Smile It would be helpful.

    Thanks Andreas.

  • tgroebner

    Ooops.Big Smile



  • Amy R Hagstrom

    Thanks.

  • Windows Latvian Langpack

    Static methods won't work.  I don't think there is any facility in native C++ to prevent hiding of base methods.

    There's always the "don't do that" answer.  Don't create B::OneFunction() if you don't want to hide A::OneFunction().  If you're worried about accidental hiding, then name the base method differently.

    Brian


  • rollingdonut

    No! This is not possible to prevent this!

  • Mike Smith-Lonergan

    Have you considered static methods Then you will know that it is your base class that gets the call.



  • emgreenberg

     David Anton wrote:
    class Base
    {
       public:
          void  OneFunction() sealed
          {
              cout<<"Base";  
          }
    };


    Oops - that won't work - you're not using C++ 2005 either...

    David Anton
    www.tangiblesoftwaresolutions.com
    Instant C#: VB.NET to C# Converter
    Instant VB: C# to VB.NET Converter
    Instant C++: C# to C++ Converter
    Instant J#: VB.NET to J# Converter
    Clear VB: Cleans up VB.NET code

  • Rick_Parker

    But in the derived class you can easily write your own OneFunction and this will be executed when OneFunction is called from the derived class.

    So again this is no solution. You can always override a function in a base class, there is no trick to prohibit this in standard C++.

  • d0mufasa

    But this is for C++ Managed Extensions. I am not using them.

    This is pure C++.

    Thanks,
    Nibu thomas.



  • ZAiNT

     Brian_Kramer wrote:
    Static methods won't work.  I don't think there is any facility in native C++ to prevent hiding of base methods.

    There's always the "don't do that" answer.  Don't create B::OneFunction() if you don't want to hide A::OneFunction().  If you're worried about accidental hiding, then name the base method differently.

    Brian



    Yeah I think this is 'a' solution.Big Smile

    Thanks brian.

    Any one with a better solution to this problem is always welcome!

    Thanks
    Nibu thomas.


  • Is there any thing equivalent to NotOverridable in C++?