CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jul 2007
    Location
    Illinois
    Posts
    517

    Empty Constructors

    Is anyone else bothered by this as much as I am? I hate them, they look like such a waste. When you have a base class that your inheriting from and it requires some arguments via its constructor but your derived class does not require anything upon construction, your required to use the derived classes constructor as a middle-man who's sole purpose in life is to just pass the arguments to the base class constructor, leaving you with an empty derived class constructor body.

    This to me is a tragedy:

    Code:
    public class Manager : Employee
    {
    
       public Manager(string name)
          : base(name) { }
    
    }
    They drive me insane sometimes to the point where Ill think of something, anything, to pass to the derived constructor to assign to a field, or call a function, or something just to make it do something other than act as a middle-man. Anyone else have this "disorder"? haha
    R.I.P. 3.5" Floppy Drives
    "I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones." - Albert Einstein

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Empty Constructors

    How about creating an empty constructor in the base class, or

    Code:
     
    public class Manager : Employee
    {
       public Manager( )
          : base( String.Empty ) { }
    }

  3. #3
    Join Date
    Jul 2007
    Location
    Illinois
    Posts
    517

    Re: Empty Constructors

    I had thought about that, but the class I'm building requires the use of an EndPoint object. Since I'm using network resources I want to force the EndPoint to be passed when the object is constructed. I want to make it immutable and not have to expose it as a public property just so I can allow it to be set later.

    I'm probably just going to have to review my code and see if I can fix it, or if I can't, its not a huge deal because the constructor in this case does actually have a little bit of work to do.
    R.I.P. 3.5" Floppy Drives
    "I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones." - Albert Einstein

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Empty Constructors

    Quote Originally Posted by RaleTheBlade
    When you have a base class that your inheriting from and it requires some arguments via its constructor but your derived class does not require anything upon construction, your required to use the derived classes constructor as a middle-man who's sole purpose in life is to just pass the arguments to the base class constructor, leaving you with an empty derived class constructor body.
    The constructor initialises the base class subobject. That makes it sound more useful than "the constructor is a middle-man whose sole purpose in life is to pass the arguments to the base class constructor"

    Besides, speaking from the point of view of C++, there is nothing wrong with having an empty constructor body. If it is feasible, I prefer to make the initialisation list do all the work.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

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