Mail Archives: djgpp/2002/02/13/16:45:14
"rb" <rbinion AT rcn DOT com> kirjoitti viestissä
news:a4a7ii$9qv$1 AT bob DOT news DOT rcn DOT net...
Thanks for your answer !
> 5. I am probably missing the overall gist of what you are trying to do.
But
> if you want the _p member of struct A to be accesible as an int, double
and
> char .. wouldn't you be better of just using a union?
Could you send me the union version of the code allso ?
>
> 6. Finaly here is roughly the equivalent C++ code with templates. Note I
> used the c io routines rather than the C++ iostreams so as not to further
> complicate the issue, made everything public for the same reason, and also
> left out other C++ idioms (like using const, ctor's, etc.).
>
> template<typename T> class holder
> {
> public:
> T value_;
> };
>
> int main(int argc,char* argv[])
> {
> int value = 66;
>
> holder<int> b = {value};
> holder<double> c = {value};
> holder<char> d = {value};
>
> printf("I am of type \"int\". My value is %i\n",(b.value_));
> printf("I am of type \"double\". My value is %f\n",(c.value_));
> printf("I am of type \"char\". My value is %c\n",(d.value_));
>
> return 0;
> }
>
>
Yeah, thats exactly what I thought people would send to me except that
there´s no constructor's & cout.
Anyway, my "great" project is to do something like this...
class Trivial : public Object
{
double _x;
double _y;
public:
Trivial():_x(10),_y(20) {}
double getX()const {return this->_x;}
double getY()const {return this->_y;}
friend ostream& operator<<(ostream& stream,const Trivial& src)
{
stream << "x:\t" << src._x << "\ty:" << src._y;
return stream;
}
};
int main(int argc,char* argv[])
{
List<Object> myList;
myList.appendEnd('A'); // "Index" position 0
myList.appendEnd(55); // 1
myList.appendEnd(-32.123); // etc...
myList.appendEnd(Trivial());
cout << myList.first()->data() << endl; // prints "A"
myList.remove(myList.first());
cout << myList.first()->data() << endl; // prints 55
myList.remove(myList.first());
cout << myList.first()->data() << endl; // prints -32.123
myList.remove(myList.first());
cout << myList.first()->data() << endl; // prints x: 10 y: 20
return(0);
}
As you can see this is heterogenic list !
It is supposed that :
A) function "myList.appendEnd" is:
template <typename Type>
List<Type>::Node* List<Type>::appendEnd(const Type& data)
{
.....
}
B) function "first()" is declared like this:
template <typename Type>
List<Type>::Node* List<Type>::first()const
{
return this->_first;
}
C) function "data()" is declared like this:
Object& List<Type>::Node::data()const // Not that it is the
"Nodes" responsibility to return the data
{
return this->_data;
}
As you can see there is this thing "List<Object> myList" and this is
exactly where we get into the subject.
I need a heterogenous object container WITHOUT the use of templates so that
I can declare a heterogenous list like above.
Note allso that there is no typecasting in the part of "cout <<
myList.first()->data() << endl" in the imaginary code.
So, if you have any advise how to accomplish this, please, maill immediately
to me
Thanks !!!
Traveler
traveler AT netti DOT fi
[ Send an empty e-mail to c++-help AT netlab DOT cs DOT rpi DOT edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
- Raw text -