Afficher un message
Vieux 07/02/2008, 19h19   #3
fabricio.olivetti@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: how to structure a class that may hold two kind of values

On 7 fev, 16:03, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
> fabricio.olive...@gmail.com wrote:
> > I am designing a class to read a data file and provide access to
> > another class, but as this dataset may contain either double or int
> > values, and some of them may be very large I'd like to create a class
> > that can decide upon allocating a vector of "char" (for the integral
> > values may be enough) or a vector of double.
> > But I can't see how can I do that...using templates I still have to
> > determine, prior the class declaration, which type this class will
> > hold.

>
> > Of course I could declare something like this:

>
> > class foo{

>
> > private:
> > vector< char > cData;
> > vector< double > dData;
> > bool type;
> > public:
> > double getData(unsigned i);
> > };

>
> > and always return a double (casting the char when required) and using
> > just the required data type using a flag to determine which type is
> > used by the class.
> > How would be the most elegant and optimized way of doing that?

>
> Without knowing how 'getData' is supposed to be used there is no way
> to tell if it fits the requirements (assumed or specified). I can
> easily think of a scenario where I'd like to have to members
>
> char getChar(unsigned i) const;
> double getDouble(unsigned i) const;
>
> and have them throw an exception if the type doesn't matcht the stored
> type of the class.
>
> V
> --
> Please remove capital 'A's when replying by e-mail
> I do not respond to top-posted replies, please don't ask


Let's say getData just returns the element on the 'i'th position of
the vector. And also, let's assume that I want to avoid a check of
what member function to call (don't wanna do a: switch(type) case 0:
getChar(); case 1: getDouble();...)
  Réponse avec citation
 
Page generated in 0,05406 seconds with 9 queries