Group: comp.lang.c++
From: Jerry Coffin
Date: Sunday, February 17, 2008 4:33 PM
Subject: Re: how to structure a class that may hold two kind of values

In article <6c3e36da-1b5f-45bd-9624-
c91c49aaa79b@p69g2000hsa.googlegroups.com>, fabricio.olivetti@gmail.com
says...

[ ... ]

> Hmm let me explain my problem further.
> I'm designin a toolbox that holds a collection of clustering
> algorithms. So let's say I'm implementing a class for algorithm A,
> another one for algorithm B and so on.
> I want the data access to be transparent for them (just use something
> like data(i,j);) whenever they need a direct access of it.
>
> The data container class will have a function to read it from a file,
> to store it in the most effective way and to perform some common
> required calculations required by the algorithms.
>
> So basically I built a GUI that gives a user the option to load a data
> from a file (and the data class will be constructed), choose one
> algorithm and this algorithm will its things with the data.

If you want to use the method you originally outlined, I'd carry it out
via inheritance:

class base_container {
public:
virtual double getData(usigned i) = 0;
};

class char_container {
vector data;
public:
double getData(unsigned i) { return data[i]; }
};

class double_container {
vector data;
public:
double getData(unsigned i) { return data[i]; }
};

base_container *data;

if (file_contains_chars) {
data = new char_container;
read_chars(file, data);
}
else {
data = new double_container;
read_doubles(file, data);
}

switch (cluster_method) {
case KM: k_means_cluster(data); break;
case SOM: self_ordered_map(data); break;
};

--
Later,
Jerry.

The universe is a figment of its own imagination.

Safety Articles | Usenet Groups | Usenet News | Bluegrass