comp.lang.ada
 help / color / mirror / Atom feed
From: Martin Krischik <martin@krischik.com>
Subject: Re: how can i allocate an objekt with initialization???
Date: Mon, 06 Dec 2004 12:55:17 +0100
Date: 2004-12-06T12:55:17+01:00	[thread overview]
Message-ID: <3208784.Wcjhki5q3f@linux1.krischik.com> (raw)
In-Reply-To: wvbry8gboi7r.fsf@sun.com

Ole-Hjalmar Kristensen wrote:

>>>>>> "MK" == Martin Krischik <martin@krischik.com> 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 <iostream>
> 
> 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



      parent reply	other threads:[~2004-12-06 11:55 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-12-05 16:54 how can i allocate an objekt with initialization??? Thomas Bruns
2004-12-05 22:51 ` Stephen Leake
2004-12-06  8:52 ` Martin Krischik
2004-12-06 10:04   ` Ole-Hjalmar Kristensen
2004-12-06 11:49     ` Adrien Plisson
2004-12-06 13:34       ` Martin Krischik
2004-12-13  6:38         ` Dave Thompson
2004-12-13 11:11           ` Martin Krischik
2004-12-06 11:55     ` Martin Krischik [this message]
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox