Hi gang,
For some reason the "search forum" bit is not working on my machine - I keep getting pitched to an error page.
Please feel free to re-direct me to a prior thread (sans flame) if this has (most likely it has) been already covered.
I am an old-time OO developer, but I came up through the glorious world of Smalltalk (can you tell where my allegiances lie?). In ST, if you wanted to make a subclass you created the subclass, and then any constructors (actually ST does not have the notion of constructors enforced), would be called automatically up the hierarchy. So if I had Class X subclassing Class A, and A implemented foo(x, y). I could create an instance of X and say X foo(1, 2). The engine would figure out the correct method to invoke.
C# does not seem to work this way... and my C++ chops are so old, I am embarrassed to say where I left off there.
How do I invoke a simple ctor in Class A from a X? Currently I have
public OutputLayer(int i, int o){
numInputs = i;
numOutputs = o;
}
public
class MiddleLayer : OutputLayer{
...
}
And I am trying to do something like
MiddleLayer ml;
ml = new MiddleLayer(layerSize[i], layerSize[i + 1]);
When I compile, I get
Error 5 'NeuralNetworks.MiddleLayer' does not contain a constructor that takes '2' arguments C:\Documents and Settings\CBurns\My Documents\Visual Studio 2008\Projects\NeuralNetworks\KSOM_Console\Network.cs 79 33 KSOM_Console
Why is the compiler not resolving to the base class' ctor?
Thanks for any input!
Chris
Christopher BurnsPosted May 28, 2008, 3:22 PM
Hi there,
Thanks for the reply!
So, if I have the 2-arg ctor implemented in the base class and I want to instantiate the subclass using the 2-arg method, I need to write a ctor in the subclass that does nothing but refer to the base class?
That fundamentally breaks inheritance. All this does is reverse-chaining, causing you to write extra code which the compiler/interpreter should provide for you as an OO architecture.
But, so be it.
Thanks again!
C
Dr SpackPosted May 28, 2008, 12:34 PM
Just look at this link: http://msdn.microsoft.com/en-us/library/ms173115(VS.80).aspx
It says that only the default constructor (ctor without any Parameters) will be called from a derived class.
Since you have added a custom ctor in your OutputLayer class you must add a ctor in MiddleLayer that calls the base ctor.
public MiddleLayer( int i, int o ) : base( i, o )
{
//...
}
In my opinion this is a correct and good behavior.
DrSpack