how to declare a variable as polymorphic?
Hello,
I would like to assign a data type to a variable first during the execution, but not at the beginning yet. How should then I declare such a variable first?
Below is my code, where I want to use it:
Code:
polymorphic Data type *net;
polymorphic Data type *netMin;
switch(which_network){
case 0: // Linear Output Feed Forward Network was chosen
net = new LinOutFFNet(input, output, connectionMatrix);
netMin = new LinOutFFNet(input, output, connectionMatrix);
break;
case 1: // Feed Forward Network was chosen
net = new FFNet(input, output, connectionMatrix);
netMin = new FFNet(input, output, connectionMatrix);
break;
case 2: // radial basis function network was chosen
net = new RBFNet(input, output, connectionMatrix);
netMin= new RBFNet(input, output, connectionMatrix);
break;
}
Thank you
best regards,
Vitali
Re: how to declare a variable as polymorphic?
I guess you need to go back to OOP basics... All you need is to have a base class, possibly pure virtual (i.e abstract, an interface, ...), for instance AbstractNet, which would define the base API of all network classes (e.g read(), write(), ...) and all other classes would inherit from that base class and reimplement the proper methods.
Then "declaring a variable as polymorphic" boils down to declaring it as a pointer to an object of the base class.
Re: how to declare a variable as polymorphic?
the problem is solved because all classes I wanted to use are subclassed from their base class, so that I can just declare a variable as a pointer to the base class...
best regards,
Vitali