dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Unique variables in derived classes

  1. #1
    Join Date
    Jan 2009
    Posts
    16

    Unique variables in derived classes

    Hey there,

    I have a class derived from another with a variable that is defined in the derived class but not in the base class.

    I can set this variable in the constructor of the derived class but then it just defaults to 0 when trying to use it in an overridden function in the derived class.

    Example:

    Code:
    public class A {
    
          protected int m_X;
    
          public A(int x) {
               m_X = x;
          }
    
          protected virtual int SomeFunction() {
               return m_X;
          }
    }
    
    public class B : A {
         private int m_Y;
    
         public B(int x, int y) : base(x) {
            m_Y = y;
         }
    
         protected override int SomeFunction()  {
             return m_Y;
         }
    }
    
    
    B myB = new B(1, 5);
    myB.SomeFunction(); //returns 0 instead of 5
    I'm sure its something silly. Can anyone help me out?

    Thanks,
    Lang

  2. #2
    Join Date
    Jan 2010
    Posts
    1,133

    Re: Unique variables in derived classes

    That shouldn't be happening. In fact, if I run the code you've provided (and only change the access level of SomeFunction() to public so it can be called outside the class), I get the correct result.

    So, either create a sample project that reproduces the problem (you might even find the solution while doing so) and post it here, or show us the actual code.

    It's probably an unintentional error - maybe you're setting the inherited variable in the derived class, where you should be setting the new one (in your example, maybe you've accidentally written m_X = y, instead of m_Y = y) - which would then cause the new variable to be initialized to its default value of zero.

    Which brings me to my next point: Protected fields (data members) should be generally avoided. The way it's now, you have increased the number of interdependencies between the base class and the derived class, and some of these are probably implicit and not at all obvious. This can cause significant problems both during developement, and worse, later on, when you try to maintain the class, or extend the hierarchy further.

    For example, take some of the extensible classes in the .NET library: none of them provides access to base class fields (data members). They do, however, have carefully designed protected methods, and their design principles are generally well documented at MSDN, so that those who create derived classes could have a roadmap of some kind.

    What I'm trying to say, you have to be careful when designing classes for inheritance - of course, this comes with experience and further study of OO design.

    In your particular case, I'd recommend to make the m_X member field private, and accessible only via a getter property defined on the base class. (I'm assuming you are familiar with C# properties?)

    I can't offer you anything more concrete than that until you show your actual code.
    If that is not an option for some reason, you could at least elaborate on what you're trying to do (not how, but what, and why - what is the end goal).

    P.S. The debugger is a great help, and it's not hard to use - search the forums on how to do it; here's how in a nutshell: F9 - set a breakpoint on some line; F5 - start debugging; F10 - execute the next line; F11 - execute the next line, or step into the next function; (hover mouse over a var) - display var info, like current value, etc.
    Last edited by TheGreatCthulhu; January 15th, 2012 at 01:40 PM.

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




On-Demand Webinars (sponsored)