473,320 Members | 1,814 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

constructor(s) for different types of input args

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
Oct 24 '07 #1
2 1733
H.S. wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 24 '07 #2
On Oct 24, 11:28 pm, "H.S." <hs.saDELETEME...@gmail.comwrote:
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.

Oct 25 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: scott | last post by:
hi people, can someone tell me, how to use a class like that* (or "simulate" more than 1 constructor) : #-- class myPointClass: def __init__(self, x=0, y=0): self.x = x self.y = y def...
3
by: Jun | last post by:
I have following script <script> var Animal = function(name){ this.name = name; } Animal.prototype.eat = function (food) {
23
by: Fabian Müller | last post by:
Hi all, my question is as follows: If have a class X and a class Y derived from X. Constructor of X is X(param1, param2) . Constructor of Y is Y(param1, ..., param4) .
7
by: hazz | last post by:
this is a repost with more concise code (well, for me) and better questions (I hope....) . given the following two classes, my intent is to use either Activator.CreateInstance or InvokeMember pass...
19
by: Martin Oddman | last post by:
Hi, I have a compiling problem. Please take a look at the code below. I have an application that is built upon three tiers: one data tier (Foo.DataManager), one business tier (Foo.Kernel) and...
0
by: Efkas | last post by:
I am trying some interresting features to load an assembly on the fly. I read some samples, but what I am trying to do is a little more complex. - First, I have a app.config file containing keys...
2
by: david | last post by:
Well, as a matter of fact I_HAD_MISSED a basic thing or two, anyway, although Ollie's answer makes perfectly sense when dealing with classes, it doesn't seem to me to apply as well if you have to...
74
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the...
6
by: =?Utf-8?B?TWF0dA==?= | last post by:
I'm having a problem with a static class constructor being called twice. I have the static class MasterTaskList which uses a BackgroundWorker to execute multiple methods on a separate thread. The...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.