From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,9fd97969641aa8c6 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII Path: g2news1.google.com!news2.google.com!proxad.net!news.cs.univ-paris8.fr!newsfeed.vmunix.org!newsfeed01.sul.t-online.de!newsmm00.sul.t-online.de!t-online.de!news.t-online.com!not-for-mail From: Martin Krischik Newsgroups: comp.lang.ada Subject: Re: how can i allocate an objekt with initialization??? Date: Mon, 06 Dec 2004 12:55:17 +0100 Organization: None Message-ID: <3208784.Wcjhki5q3f@linux1.krischik.com> References: <3459883.9H2ke8dEzz@linux1.krischik.com> Reply-To: martin@krischik.com Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-15 Content-Transfer-Encoding: 8Bit X-Trace: news.t-online.com 1102334597 04 22159 N4KMXVnLABn-soz 041206 12:03:17 X-Complaints-To: usenet-abuse@t-online.de X-ID: Vrlvr4ZUZeDkgSZlzCLX6BVh+8RPQvQScIPaFQ0A8MtCUeUDjDEZ8P User-Agent: KNode/0.8.0 Xref: g2news1.google.com comp.lang.ada:6797 Date: 2004-12-06T12:55:17+01:00 List-Id: Ole-Hjalmar Kristensen wrote: >>>>>> "MK" == Martin Krischik writes: > > MK> Thomas Bruns wrote: > >> TEST : FATHER_CLASS_PTR; > >> > >> begin > >> TEST:= new CHILD_CLASS'( INT=>1); -- i will initialze the objekt > >> here,but > > MK> Since you are initializing an element form the parent class you > need to MK> initialize the child as well: > > MK> TEST:= new CHILD_CLASS'( > MK> INT1 =>1 > MK> INT2 =>1); > > MK> only the other way round you can shortcut: > > MK> TEST:= new CHILD_CLASS'( > MK> FATHER_CLASS > MK> WITH > MK> INT2 =>1); > > MK> And the standart question for beginners: Are you shure you need a > pointer? MK> Unlike C/C++/Java Ada allows for: > > MK> TEST : FATHER_CLASS'Class :=�CHILD_CLASS'( > MK> INT1 =>1 > MK> INT2 =>1); > > MK> You can use inheritance in Ada without using pointers! > FATHER_CLASS'Class MK> can be used just like a String - you can pass > it around as parameter, MK> return it from a function, store it in a > record. There are collection MK> libraries where you can store them. > > You are confused. You can use inheritance and virtual functions in C++ > as well without using pointers: Nice excample - but there are two little problems with it: > #include > > class a > { > private: > int _value; > public: > a(int x) : _value(x) {} > int n(){return _value;} > virtual void f() = 0; > }; > > class b : public a > { > public: > b(int x) : a(x) {} > void f(); > }; > > void b::f() > { > std::cout << "I am b " << n() <<"\n"; > } > > class c : public a > { > public: > c(int x) : a(x) {} > void f(); > }; > > void c::f() > { > std::cout << "I am c " << n() << "\n"; > } > > void test(a& x) But '&' is only special type pointer with the attributes of "constant target", "automatic derefence" and "never be null". But still a pointer. Try yourself: a& pointer = *new B; delete &A; > { > x.f(); > } > > int main(int argc, char *argv[]) > { > b my_b(1); > c my_c(2); To match my example you need to say: a my_b(1); a my_c(2); but that is not possible bacause C++ has no conzept of indefinite types. Only C99 is doing some very snall steps into that direction. > test(my_b); > test(my_c); > > return 0; > } BTW: I have 10+ profesional experience in C/C++ but I read comp.lang.ada in my spare time. So my state of mind is rather "disilusioned" and not "confused". With Regards Martin -- mailto://krischik@users.sourceforge.net http://www.ada.krischik.com