comp.lang.ada
 help / color / mirror / Atom feed
From: Simon Wright <simon@pushface.org>
Subject: Re: Constructors with multiple inheritance
Date: Thu, 10 Nov 2011 08:33:51 +0000
Date: 2011-11-10T08:33:51+00:00	[thread overview]
Message-ID: <m2d3d0bcls.fsf@pushface.org> (raw)
In-Reply-To: 6366850.176.1320896821400.JavaMail.geo-discussion-forums@yqcm23

"Rego, P." <pvrego@gmail.com> writes:

> Maybe I can explain this easier trying to use C++. Let's suppose I
> have a class Par_Class with an integer and a pointer to a string. I
> could code, for example, (please correct me if I'm doing wrong)
>
> class Par_Class {
> public:
>   Par_Class (int aValue, const char *aName);
>
> private:
>   int theValue;
>   char *theName;
> };
>
> the constructor could be implemented as
>
> Par_Class::Par_Class (int aValue, const char *aName){
>   theValue = aValue;
>   strcpy (theName, aName);
> };
>
> and finally we could instantiate this class with
>
> Par_Class Par_Obj (23, "My object is this");
>
> and sure this constructor method belongs to the class Par_Class and
> not to any other class.

Par_Class (int aValue, const char *aName) is the very special C++
constructor syntax; when used the compiler generates a new blank area of
memory and the constructor gets to fill it in.

This is *not* the same as 

   function Construct 
     (P : access Par_Class; aValue : Integer; aName : Integer_Ptr)
      return Par_Class_Ptr;

which requires there to be a previous Par_Class instance; the nearest
(Ada 2012) equivalent is I think

   function Construct 
     (aValue : Integer; aName : Integer_Ptr)
      return Par_Class is
   begin
      return Result : Par_Class do
         Result.theValue := aValue;
         Result.theName := aName;
      end return;
   end Construct;

or, OK for Ada 95 and nearest to your present scheme:

   function Construct 
     (aValue : Integer; aName : Integer_Ptr)
      return Par_Class_Ptr is
      P_Ptr : constant Par_Class_Ptr := new Par_Class;
   begin
      P_Ptr.theValue := aValue;
      P_Ptr.theName := aName;
      return P_Ptr;
   end Construct;



  parent reply	other threads:[~2011-11-10  8:33 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-09-29 19:40 Constructors with multiple inheritance Rego, P.
2011-09-29 20:20 ` Dmitry A. Kazakov
2011-09-30  3:11   ` Rego, P.
2011-09-30  7:36     ` Dmitry A. Kazakov
2011-09-30 14:04       ` Rego, P.
2011-09-30 16:29         ` Robert A Duff
2011-09-30 19:14           ` Rego, P.
2011-09-30 16:42         ` Dmitry A. Kazakov
2011-09-30 19:42           ` Rego, P.
2011-10-06 12:46             ` Julian Leyh
2011-11-09  2:24           ` Rego, P.
2011-11-09  8:39             ` Dmitry A. Kazakov
2011-11-10  3:47               ` Rego, P.
2011-11-10  7:09                 ` AdaMagica
2011-11-10  7:20                   ` AdaMagica
2011-11-10  8:35                   ` Dmitry A. Kazakov
2011-11-12 15:16                     ` Rego, P.
2011-11-12 15:30                   ` Rego, P.
2011-11-12 16:28                     ` Dmitry A. Kazakov
2011-11-12 17:41                       ` Rego, P.
2011-11-10  8:33                 ` Simon Wright [this message]
2011-11-10  9:01                 ` Georg Bauhaus
2011-11-10  9:09                   ` Georg Bauhaus
2011-11-10 18:16                 ` Jeffrey Carter
2011-11-10 19:39                   ` Dmitry A. Kazakov
2011-11-09  9:00             ` Simon Wright
2011-11-10  3:54               ` Rego, P.
2011-10-07  0:08 ` Shark8
replies disabled

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