Click to See Complete Forum and Search --> : Empty Constructors


RaleTheBlade
November 18th, 2009, 03:06 PM
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:


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

Arjay
November 19th, 2009, 10:35 AM
How about creating an empty constructor in the base class, or


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

RaleTheBlade
November 19th, 2009, 01:54 PM
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.

laserlight
November 27th, 2009, 05:24 AM
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" :D

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.