CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jan 2003
    Location
    Massachusetts
    Posts
    170

    Question stoping derived class methods from being called?

    Hi all, I have a situation where I want to add a method to a base class and make sure that no derived class can override it. Easier said than done! The problem is that we have a few hundred classes that have already overrided the method and I want to turn them all off with out editing hundreds of source files. Is there a way to do this?

    thanks!

    Jim
    I am scifi

  2. #2
    Andy Tacker is offline More than "Just Another Member"
    Join Date
    Jun 2001
    Location
    55°50' N 37°39' E
    Posts
    1,503
    use keyword "private"

    private void Myfunction()
    {}
    Last edited by Andy Tacker; December 11th, 2003 at 02:12 AM.
    If you think you CAN, you can, If you think you CAN'T, you are probably right.

    Have some nice Idea to share? Write an Article Online or Email to us and You may WIN a Technical Book from CG.

  3. #3
    Join Date
    Jan 2003
    Location
    Massachusetts
    Posts
    170
    Originally posted by Andy Tacker
    use keyword "private"

    private void Myfunction()
    {}
    Hi Andy. I am using the private keyword. The base class method is called first, then the derived class method. I need to stop program flow from going to the derived class method without getting build errors.

    Thanks!

    jim
    I am scifi

  4. #4
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125
    If you want to prevent a method from being overridden (which requires the virtual keyword on the base, and the override keyword on the derived), then simply mark the method as sealed.

    Of course this does not address two issues that you have:

    1) You are probably hiding methods in the derived class rahter than overriding them.

    2) This is a design time decision and will cause all of your derived class to fail to compile if they attempt to override the method.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  5. #5
    Join Date
    Jan 2003
    Location
    Massachusetts
    Posts
    170
    Hi, I added the sealed keyword to my base class method definition and I get this compile error:

    "cannot be sealed because it is not an override"

    I can not add the override keyword because this is not an overrided method, it is the base method.

    I can go thru all of our source files and remove all of the overrided method definitions but then I need a way to stop this method from being overrided again in the derived classes.

    I'll continue searching msdn but so far I have had no luck.

    thanks!

    Jim
    I am scifi

  6. #6
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125
    If you are getting that message, then the function is NOT an override, it is a totally new mothod that HIDES the previous method.

    Typically (although not always) this is an indication of a MAJOR design flaw in the code.

    Consider what happens if a derived object is assigned to a variable of the derived type and of the base type. Invoking the same method name on the same object will have different behaviour!

    Please give serious thought to performing a complete design review of your architecture. Quick fixes (like the one you just mentioned) are more likely to just change the problem than to solve it!
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  7. #7
    Join Date
    Jan 2003
    Location
    Massachusetts
    Posts
    170
    welp, *sigh* unfortunately I have no control over the design. I agree with you that there is an inherent design flaw because we are not using the new model. Luckily this is only a demo and we will address these design flaws later. I should think that if Microsoft wants us to use abstract and override keywords then they should have put something in place to not allow code like ours to compile and force us to follow the new model. But I digress.

    I am not sure what Hiding means. It sounds like if I override a method without using the correct keywords then one of the methods should not get called but in my case both are being called, first the base class method is called then the derived method. Maybe something else is going on here?

    At any rate it seems that all I can do for now is remove the derived methods and hope no one in my shop tries to override the method. :-D

    I think I will override Murphy's Law: "If something can be coded it will be coded"

    Thanks!!

    Jim
    I am scifi

  8. #8
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125
    Regardless of the keywords used, just as in c+++, you should not see both routines being called (unless the derived calls the base). On the other hand, if they are event procedures, this is the intended effect regardless of the keywords being used.

    The following example shows a good exampe of hiding in classic c++ (just to prove it is not a "new" issue)

    Code:
    class Base
    {
      virtual void F1() { printf("B:F1()"); }
      void F2() { printf("B:F2()"); }
      void F3() { F1(); F2(); }
      virtual void F4() { F1(); F2(); F3(); }
      void F5() { F1(); F2(); F3(); F4(); }
    }
    
    class Derived : public Base
    {
      void F1() { printf("D:F1()"); }
      void F2() { printf("D:F2()"); }
      void F3() { F1(); F2(); }
      void F4() { F1(); F2(); F3(); }
      void F5() { F1(); F2(); F3(); F4(); }
    }
    
    void main()
    {
    Base *p1 = new Base();
    Base *p2 = new Derived();
    Derived *p3 = new Derived();;
    p1->F1(); p1->F2();p1->F3();p1->F4();p1->F5();
    p2->F1(); p2->F2();p2->F3();p2->F4();p2->F5();
    p3->F1(); p3->F2();p3->F3();p3->F4();p3->F5();
    }
    The results often suprise people who do not understand the difference between overrifing and hiding.

    Since C++ gives no indication when these effects occur, C# is actually an improvement by providing the override, sealed and new keywords so that you must explicitly state your intentions.

    Hope this helps.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured