X-Authentication-Warning: delorie.com: mailnull set sender to djgpp-bounces using -f From: "Traveler" Newsgroups: comp.lang.c++,comp.lang.c++.moderated,comp.os.msdos.djgpp Subject: Re: Heterogenous object container without templates & type casting ??? Date: 13 Feb 2002 16:35:05 -0500 Organization: SAUNALAHDEN asiakas Lines: 127 Sender: cppmods AT netlab DOT cs DOT rpi DOT edu Approved: kuehl AT fmi DOT uni-konstanz DOT de Message-ID: References: NNTP-Posting-Host: netlab.cs.rpi.edu X-Original-Date: Wed, 13 Feb 2002 20:17:48 +0200 X-Submission-Address: c++-submit AT netlab DOT cs DOT rpi DOT edu X-Auth: PGPMoose V1.1 PGP comp.lang.c++.moderated iQBVAwUAPGrcBUHMCo9UcraBAQETNgH/eYBLJtpEvxC/PUcySCcfjVx/YxCw1Gv7 3Hs3xXINWYU1mRnx9SE/xBOBBuFN+OJ8Sw/8y38XzETNWM0+uxr0ig== =4/Pk To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com "rb" 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 class holder > { > public: > T value_; > }; > > int main(int argc,char* argv[]) > { > int value = 66; > > holder b = {value}; > holder c = {value}; > holder 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 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 List::Node* List::appendEnd(const Type& data) { ..... } B) function "first()" is declared like this: template List::Node* List::first()const { return this->_first; } C) function "data()" is declared like this: Object& List::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 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! ]