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

Thread: Multiple Inheritance and conctructors

  1. #1
    Join Date
    Sep 2009
    Posts
    1

    Multiple Inheritance and conctructors

    I'm working on a project and I need some help with C# constructors. Let's say I have the following;

    Code:
    public class vehicle
    {
       public vehicle()
      {
        //do some initialization for all vehicles
      }
    }
    
    public class automobile : vehicle
    {
       public auto : base
      {
        // do specific initialization for auto
      }
    }
    
    public class truck : auto
    {
       public truck : base
      {
        // do specific initialization for truck
      }
    }
    Now, I want truck to inherit from from automobile because there are methods that I want to reuse. However, I do not want to the call the auto constructor, I want to call the vehicle constructor instead.

    Anyone know of a way I can do that?

    Thanks in advance.

  2. #2
    Join Date
    Oct 2007
    Posts
    16

    Re: Multiple Inheritance and conctructors

    I am pretty sure that there are no supported way of doing this directly, but you can achieve it via a parametrized constructor in auto that will simply forward to the base class

    Code:
    public class automobile : someCommonClass
    {
       public auto : base
      {
        // do specific initialization for auto
      }
       public auto(bool bDummy) : base()
      {
        // do nothing
      }
    }
    
    public class truck : someCommonClass
    {
       public truck : base(true)
      {
        // do specific initialization for truck
      }
    }
    However, this is not very clean, I would suggest that if you share a part of the logic between auto and truck that they both inherit a common class with the given functionnality


    Code:
    public class vehicle
    {
       public vehicle()
      {
        //do some initialization for all vehicles
      }
    }
    
    public class someCommonClass : public vehicle
    {
        // code common to both auto and truck
        public someCommonClass : base()
       {
          // initialization common to both auto and truck
       }
    }
    public class automobile : someCommonClass
    {
       public auto : base
      {
        // do specific initialization for auto
      }
    }
    
    public class truck : someCommonClass
    {
       public truck : base
      {
        // do specific initialization for truck
      }
    }
    Hope this helps.

  3. #3
    Join Date
    Jul 2006
    Posts
    297

    Re: Multiple Inheritance and conctructors

    Why use a common class theres nothing wrong with inheritance here.

    A truck is an automobile a automobile is a vehicle... straight down the inheritance line theres no problems here. Keep it like this. All you need to do to NOT call the constructor for the automobile class is to leave off the : base() from the constructor of the truck class.

    Code:
    public class vehicle
    {
       public vehicle()
      {
        //do some initialization for all vehicles
      }
    }
    
    public class automobile : vehicle
    {
       public automobile() : base()
      {
        // do specific initialization for auto
      }
    }
    
    public class truck : automobile 
    {
      public truck()
      {
        // do specific initialization for truck
      }
    }

  4. #4
    Join Date
    Oct 2007
    Posts
    16

    Re: Multiple Inheritance and conctructors

    However, I do not want to the call the auto constructor, I want to call the vehicle constructor instead.
    I thinks he wants to call vehicle constructor, hence my suggestion for two classes. As I said there are many ways to achieve this other than adding a new class, but not wanting to call the constructor of your base class could be a sign that along the way they will differ more and more.

    What you need to evaluate is if truck will use 99% of the logic of automobile or just a subset of it. From there you can make the decision of adding a class or leaving your hierarchy as it is.

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)