fabricio.olivetti@gmail.com wrote:
> 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();...)
That's not really a design specificiation. It looks very much like
an implementation detail. Now, the check you are talking about has
to be done inside 'getData' anyway. So, it doesn't really matter
who makes it since it has to be made.
Now, perhaps you will think a bit what the importance of the dual
storage is and what the use of 'getData' is, and then (you don't
really have to tell us) you will have a clearer picture why you
think you need the two vectors and one 'getData' function. Who
stores the vectors? Who sets the 'type'? How are they changed?
And, most importantly, why?
And, if you can it, please don't quote signatures. Thanks!
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask