comp.lang.ada
 help / color / mirror / Atom feed
* how can i allocate an objekt with initialization???
@ 2004-12-05 16:54 Thomas Bruns
  2004-12-05 22:51 ` Stephen Leake
  2004-12-06  8:52 ` Martin Krischik
  0 siblings, 2 replies; 9+ messages in thread
From: Thomas Bruns @ 2004-12-05 16:54 UTC (permalink / raw)


Hello

i have a problem, with initialzie objekt with ada...

i want to do a new, to allocate the objekt... 

--------------------------------------------------------------
with ADA.FINALIZATION; use ADA.FINALIZATION;
package class_test_package is

 type FATHER_CLASS is new CONTROLLED with
  record
   INT1 : INTEGER;
  end record;

 type FATHER_CLASS_PTR is access FATHER_CLASS'Class;
 
 function GETINT1 return INTEGER is abstract;

end class_test_package;

with class_test_package; use class_test_package;
package class_test_package_ableitung is

type CHILD_CLASS is new FATHER_CLASS with private;

type CHILD_CLASS_PTR is access CHILD_CLASS'CLASS;
private

 procedure Initialize (OBJECT : in out KIND_CLASS);
 
 procedure Finalize  (OBJECT : in out KIND_CLASS);
 
 procedure ADJUST (OBJECT : in out KIND_CLASS);
  
 type CHILD_CLASS is new FATHER_CLASS with
  record
   INT2 : INTEGER;
  end record;

end class_test_package_ableitung;


main:

TEST : FATHER_CLASS_PTR;

begin
 TEST:= new CHILD_CLASS'(INT=>1); -- i will initialze the objekt here,but 
       -- it wrong??
--class_test.adb:11:57: expected private type "KIND_CLASS" defined at
--class_test_package_ableitung.ads:16
--class_test.adb:11:57: found a composite type

end;



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: how can i allocate an objekt with initialization???
  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
  1 sibling, 0 replies; 9+ messages in thread
From: Stephen Leake @ 2004-12-05 22:51 UTC (permalink / raw)
  To: comp.lang.ada

Thomas Bruns <newsgroup@donbruno.de> writes:

> Hello
> 
> i have a problem, with initialzie objekt with ada...
> 
> i want to do a new, to allocate the objekt... 

Here's a version of your code that compiles (except it is missing a
package body):

with ADA.FINALIZATION; use ADA.FINALIZATION;
package class_test_package is

 type FATHER_CLASS is new CONTROLLED with
  record
   INT1 : INTEGER;
  end record;

 type FATHER_CLASS_PTR is access all FATHER_CLASS'Class;

 function GETINT1 return INTEGER is abstract;

end class_test_package;

with class_test_package; use class_test_package;
package class_test_package_ableitung is

type CHILD_CLASS is new FATHER_CLASS with private;

type CHILD_CLASS_PTR is access CHILD_CLASS'CLASS;

function Factory (Int1, Int2 : in Integer) return Child_Class_Ptr;

private

 procedure Initialize (OBJECT : in out Child_CLASS);

 procedure Finalize  (OBJECT : in out CHILD_CLASS);

 procedure ADJUST (OBJECT : in out CHILD_CLASS);

 type CHILD_CLASS is new FATHER_CLASS with
  record
   INT2 : INTEGER;
  end record;

end class_test_package_ableitung;

with Class_Test_Package;
with Class_Test_Package_Ableitung;
procedure Main is

TEST : Class_Test_Package.FATHER_CLASS_PTR;

begin
 TEST := Class_Test_Package.FATHER_CLASS_PTR
    (Class_Test_Package_Ableitung.Factory (INT1 => 1, Int2 => 2));

end Main;


Your problem was that Child_Class is a private type; you cannot create
an aggregate for it. The body of Factory can use an aggregate.

Hope this helps.

-- 
-- Stephe




^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: how can i allocate an objekt with initialization???
  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
  1 sibling, 1 reply; 9+ messages in thread
From: Martin Krischik @ 2004-12-06  8:52 UTC (permalink / raw)


Thomas Bruns wrote:

> TEST : FATHER_CLASS_PTR;
> 
> begin
>  TEST:= new CHILD_CLASS'( INT=>1); -- i will initialze the objekt here,but

Since you are initializing an element form the parent class you need to
initialize the child as well:

TEST:= new CHILD_CLASS'(
                         INT1 =>1
                         INT2 =>1);

only the other way round you can shortcut:

TEST:= new CHILD_CLASS'(
                         FATHER_CLASS
                     WITH
                         INT2 =>1); 

And the standart question for beginners: Are you shure you need a pointer?
Unlike C/C++/Java Ada allows for:

TEST : FATHER_CLASS'Class  :=ï¿œCHILD_CLASS'(
                         INT1 =>1
                         INT2 =>1);

You can use inheritance in Ada without using pointers! FATHER_CLASS'Class
can be used just like a String - you can pass it around as parameter,
return it from a function, store it in a record. There are collection
libraries where you can store them.

With Regards

Martin
-- 
mailto://krischik@users.sourceforge.net
http://www.ada.krischik.com



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: how can i allocate an objekt with initialization???
  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 11:55     ` Martin Krischik
  0 siblings, 2 replies; 9+ messages in thread
From: Ole-Hjalmar Kristensen @ 2004-12-06 10:04 UTC (permalink / raw)


>>>>> "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:

#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)
{
  x.f();
}

int main(int argc, char *argv[])
{
  b my_b(1);
  c my_c(2);

  test(my_b);
  test(my_c);

  return 0;
}
    MK> With Regards

    MK> Martin
    MK> -- 
    MK> mailto://krischik@users.sourceforge.net
    MK> http://www.ada.krischik.com

-- 
   C++: The power, elegance and simplicity of a hand grenade.



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: how can i allocate an objekt with initialization???
  2004-12-06 10:04   ` Ole-Hjalmar Kristensen
@ 2004-12-06 11:49     ` Adrien Plisson
  2004-12-06 13:34       ` Martin Krischik
  2004-12-06 11:55     ` Martin Krischik
  1 sibling, 1 reply; 9+ messages in thread
From: Adrien Plisson @ 2004-12-06 11:49 UTC (permalink / raw)


Ole-Hjalmar Kristensen wrote:
> You are confused. You can use inheritance and virtual functions in C++
> as well without using pointers:

you are confused too... see below.

> int main(int argc, char *argv[])
> {
>   b my_b(1);
>   c my_c(2);
> 
>   test(my_b);
>   test(my_c);
> 
>   return 0;
> }

your complete example is valid (though a little bit off-topic), but the OP was 
trying to declare a variable which can hold any object from the class hierarchy 
FATHER_CLASS and initialize it with a CHILD_CLASS object.

the corresponding C++ example should have been:

int main(int argc, char *argv[])
{
   a &my_object = b(1);
   my_object.f();

   return 0;
}

note: Martin Krischik was noting that in Ada we can avoid pointers/references 
to do this, unlike in C++. our 2 examples just confirm that (plus it does not 
help much...).

-- 
rien



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: how can i allocate an objekt with initialization???
  2004-12-06 10:04   ` Ole-Hjalmar Kristensen
  2004-12-06 11:49     ` Adrien Plisson
@ 2004-12-06 11:55     ` Martin Krischik
  1 sibling, 0 replies; 9+ messages in thread
From: Martin Krischik @ 2004-12-06 11:55 UTC (permalink / raw)


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



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: how can i allocate an objekt with initialization???
  2004-12-06 11:49     ` Adrien Plisson
@ 2004-12-06 13:34       ` Martin Krischik
  2004-12-13  6:38         ` Dave Thompson
  0 siblings, 1 reply; 9+ messages in thread
From: Martin Krischik @ 2004-12-06 13:34 UTC (permalink / raw)


Adrien Plisson wrote:

> Ole-Hjalmar Kristensen wrote:
> the corresponding C++ example should have been:
> 
> int main(int argc, char *argv[])
> {
>    a &my_object = b(1);

I have seen this before and I am wondering if it is actualy valid. Because:
where is the result of the b(1) constructor call been stored? a& can only
hold a reference to retun value. Is this not a variation on the classic
mistake.

int&
f ()
  {
  auto int retval = 5;

  // do something

  return retval;
  }

and that would mean that the livetime of the constructor call result is
undefined.

>    my_object.f();
> 
>    return 0;
> }
> 

Martin

PS: The above problem case can actually happen to anyone - when hidden by
implicid type convertion

extern int &f(); -- returns valid reference

unsigend&
g ()
  {
  return f();  -- just delegate.
  }

IBM C++ will convert int& to unsigned& - works fine
MS C++ will convert int& to int to unsigned to unsigned& - :-((

-- 
mailto://krischik@users.sourceforge.net
http://www.ada.krischik.com



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: how can i allocate an objekt with initialization???
  2004-12-06 13:34       ` Martin Krischik
@ 2004-12-13  6:38         ` Dave Thompson
  2004-12-13 11:11           ` Martin Krischik
  0 siblings, 1 reply; 9+ messages in thread
From: Dave Thompson @ 2004-12-13  6:38 UTC (permalink / raw)


On Mon, 06 Dec 2004 14:34:13 +0100, Martin Krischik
<martin@krischik.com> wrote:

> > Ole-Hjalmar Kristensen wrote:
> > the corresponding C++ example should have been:

> >    a &my_object = b(1);
> 
> I have seen this before and I am wondering if it is actualy valid. Because:
> where is the result of the b(1) constructor call been stored? a& can only
> hold a reference to retun value. 

In a temporary which persists as long as the reference, 12.2p5. In
practice this means the stack frame is extended. Since C++ references
cannot be reseated, and the type of an initializer is AFAICS always
statically known, this isn't terribly valuable, but it does work.

> Is this not a variation on the classic > mistake.
> 
> int&
> f ()
>   {
>   auto int retval = 5;
> 
>   // do something
> 
>   return retval;
>   }
> 
That's different. auto objects explicitly have lifetime of the block
in which they are declared hence allocated. And since under the covers
the reference is only a pointer, this returns a stale pointer.

(Aside: auto is the default storage class where it applies, to local
variables, so does not need to be and usually is not given explicitly.
Especially since in C++ and C99 'implicit int' is gone and the type
specifier must be present in the beginning part of a declaration. In
fact there have been semiserious proposals to 'recycle' the keyword
'auto' to a new meaning since its existing one is so unnecessary.)
- David.Thompson1 at worldnet.att.net



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: how can i allocate an objekt with initialization???
  2004-12-13  6:38         ` Dave Thompson
@ 2004-12-13 11:11           ` Martin Krischik
  0 siblings, 0 replies; 9+ messages in thread
From: Martin Krischik @ 2004-12-13 11:11 UTC (permalink / raw)


Dave Thompson wrote:

> On Mon, 06 Dec 2004 14:34:13 +0100, Martin Krischik
> <martin@krischik.com> wrote:
> 
>> > Ole-Hjalmar Kristensen wrote:
>> > the corresponding C++ example should have been:
> 
>> >    a &my_object = b(1);
>> 
>> I have seen this before and I am wondering if it is actualy valid.
>> Because: where is the result of the b(1) constructor call been stored? a&
>> can only hold a reference to retun value.
> 
> In a temporary which persists as long as the reference, 12.2p5.

You live and learn!

> (Aside: auto is the default storage class where it applies, to local
> variables, so does not need to be and usually is not given explicitly.
> Especially since in C++ and C99 'implicit int' is gone and the type
> specifier must be present in the beginning part of a declaration. In
> fact there have been semiserious proposals to 'recycle' the keyword
> 'auto' to a new meaning since its existing one is so unnecessary.)

Having learned pascal and modula 2 before C and C++ I don't consider it
unnecessary. I consider it good for readability. If I see "auto" I know a
variable is declared. If I just see

"some_very_long_something somthing_else_after_a_tiny_space;"

It's not quite as easy to see. 

Martin
-- 
mailto://krischik@users.sourceforge.net
http://www.ada.krischik.com



^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2004-12-13 11:11 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox