BCB Mailing List

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[BCB] A class problem -- A die hard challenge



Hi...

The class should simulate dynamic types. We want to design a class whose
objects could potentially of any reasonably well-behaved type. In other
words, suppose we want to put objects of several unrelated types into one
container. How might we arrange for that ability? Designing a  container
speciffically for this purpose won't do, because we would like to have a
single design independent of the application.

Note that this is not a part of an assessment or homework or the like. I am
trying to simulate some good features of the language Eiffel with C++.
Eiffel support a data type called Any that the compiler doesn't know the
type of the variable until it is assigned a value. That value determine the
type of the object.

I think the word 'container' may yield to misunderstanding. What I asking
for something like the following code

Any three(3);
Any pi(3.14159);
int n;
if (three.extract(n))
  cout << n << endl;  // prints 3
pi.extract(n); //has no effect; yields false

such that three can contain an int value and pi can contain a double value,
even though three and p have the same type. The extract member obtains data
from an Any when given a guess as to type that the object contains. Because
three contains an int, calling three.extract(n) copies the value of that
int into n and yields true. On the other hand, calling pi.extract(n) yields
false and has no effect.

Indeed I solved part of the problem. But the most challenging part is to
overload assignment and copy the values.


Mohammad Iqbal



W Komornicki's Home Page | Main Index | Thread Index