constructor(s) for different types of input args

H

H.S.

Hello,

I have written a numerical library and currently the constructor assumes
that the input data is of a particular type (data is 2D array). I would
like to add the functionality that the input data be given as a file as
well. Here is an abbreviated constructor I am using:

hsc_lib::hsc_lib(TNT::Array2D <double> & X, int iK, int id):
var1(iK),var2(id),var3(NULL),var4(false)
{
/*some internal parameter initializations and mem allocations occur here*/
}

Any suggestions how I can go about inputing the data as a file as well
(in addition to that 2D array)? One approach is to overload this
constructor. But this involves copying everything in it to another
constructor and I then need to be very careful for making modifications
to the constructors in future.

Any other suggestions?

thanks,
->HS
 
V

Victor Bazarov

H.S. said:
I have written a numerical library and currently the constructor
assumes that the input data is of a particular type (data is 2D
array). I would like to add the functionality that the input data be
given as a file as well. Here is an abbreviated constructor I am
using:

hsc_lib::hsc_lib(TNT::Array2D <double> & X, int iK, int id):
var1(iK),var2(id),var3(NULL),var4(false)
{
/*some internal parameter initializations and mem allocations occur
here*/ }

Any suggestions how I can go about inputing the data as a file as well
(in addition to that 2D array)? One approach is to overload this
constructor. But this involves copying everything in it to another
constructor and I then need to be very careful for making
modifications to the constructors in future.

Any other suggestions?

See the Decorator pattern or Factory pattern (or Factory Method). You
basically make another class (or function) which will construct the
object of your type from the file by reading the file and providing the
arguments to the constructor. The object then is returned to you:

BlahClass blah = BlahClass::constructFromFile(filename);

class BlahClass {
BlahClass(array, sizes, other_stuff);
static BlahClass constructFromFile(const char* fn);
};

Of course, the point would be to provide either a very good copy-
constructor or rely on compiler optimizing the copy away.

V
 
I

Ioannis Gyftos

Hello,

I have written a numerical library and currently the constructor assumes
that the input data is of a particular type (data is 2D array). I would
like to add the functionality that the input data be given as a file as
well. Here is an abbreviated constructor I am using:

hsc_lib::hsc_lib(TNT::Array2D <double> & X, int iK, int id):
var1(iK),var2(id),var3(NULL),var4(false)
{
/*some internal parameter initializations and mem allocations occur here*/

}

Any suggestions how I can go about inputing the data as a file as well
(in addition to that 2D array)? One approach is to overload this
constructor. But this involves copying everything in it to another
constructor and I then need to be very careful for making modifications
to the constructors in future.

Any other suggestions?

thanks,
->HS

I guess you can overload your constructor, as you say, and move the
shared part of the code to a private member function, so you can ease
maintenance.

On another note, if your constructor loads data from a file, take care
of exceptions (e.g. when file does not exist). However I am not
experienced enough to advise on anything on this matter.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top