Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6790011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:40:26+00:00 2026-05-26T17:40:26+00:00

Maybe the C++ and Java people can help me to define this problem I’m

  • 0

Maybe the C++ and Java people can help me to define this problem I’m going to explain. I have a problem in Ada (you don’t need to know it, I’m just interested in the concept) on how representing a Constructor of a class which implements three main branches of dynamic identifiers:

  • Pure number values (int, float, String, whatever)
  • List/stack item
  • Something what in C++ is likely a thread (in Ada we have a more wide concept of this, related to a task, but we can concept a simple task as a thread, so the concept applies too)

I’m gonna call this class Par_Class, and be any constructed object call Par_Obj. Thus, when an object Par_Obj is created (so, the number values are initialized, the lists/stacks have other lists/stacks allocated or null and the memory range for the thread execution is reserved), the OS automatically starts the execution of the new thread in parallel with my main application (and now they contend for system resources). But to simplify the example, let’s suppose I’d have a class with an integer and a pointer to a string.

In C++, I could code, for example, (please correct me if I’m doing wrong)

class Par_Class {
public:
  Par_Class (int aValue, const std::string & aName);

private:
  int theValue;
  std::string theName;
};

the constructor could be implemented as

Par_Class::Par_Class (int aValue, const std::string & aName)
  : theValue(aValue)
  , 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.

Similarly, in Java, we could code

public class Par_Class {
  private int theValue;
  private String theName;

  public Par_Class (int aValue, String aName){
    theValue = aValue;
    theName = aName;
  }
};

and we could instantiate the object using

Par_Class Par_Obj = new Par_Class (23, "My object is this");

(again please correct me if I’m wrong). Again, Par_Class constructor is a method of the class Par_Class.

In Ada 2005, this class could be coded as

--par_pkg.ads
package Par_Pkg is
   type Par_Class is tagged private;
   type Par_Class_Ptr is access all Par_Class;
   type Integer_Ptr is access Integer;

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

private
   type Par_Class is tagged
      record
         theValue : Integer;
         theName  : Integer_Ptr;
      end record;
end Par_Pkg;

-- par_pkg.adb
package body Par_Pkg is
   function Construct 
     (P : access Par_Class; aValue : Integer; aName : Integer_Ptr)
      return Par_Class_Ptr is
      pragma Unreferenced (P);
      P_Ptr : constant Par_Class_Ptr := new Par_Class;
   begin
      P_Ptr.theValue := aValue;
      P_Ptr.theName := aName;
      return P_Ptr;
   end Construct;

end Par_Pkg;

and the user could call

with Par_Pkg; use Par_Pkg;
procedure Par_Main is
   Par_Obj : Par_Class_Ptr;
   Int_Obj : Integer_Ptr;
begin
   Int_Obj := new Integer;
   Int_Obj.all := 12; -- don't worry about been string or integer
   Par_Obj := Par_Obj.Construct 
     (aValue => 23,
      aName => Int_Obj);
end Par_Main;

And that’s where resides the problem. The compiler says me that I could not use the method Construct in Par_Obj := Par_Obj.Construct because yet my object is null. But it’s so obvious, because just what I want to do is to initialize the object (so it would not be null anymore). There are other ways of constructing the object, for example, using a function from outside the class, but I don’t want to use this approach because it runs away from architecture. Could you please help me to formulate the problem to my Ada friends so they can help me to implement it in Ada? I guess I’m having a bit difficult on explaining this in general concept terms. Thanks.

Answer

@paercebal gave me what I think could achieve my goal:

  • “Is there a way to have a “static” function declared inside Par_Class?” and “is there a way to have an non-member function declared friend of Par_Class?”

I could complete it with “is there a way to have a “static” function declared inside a tagged type? Also, could the package where the class is declared act as a friend or as a static function?”

Update

Got some more good reasons on why implementing it as suggested by @SimonWright and some people from comp.lang.ada forum:

function Construct (aValue: Integer; aName: Integer)
                    return Par_Class is
begin
  return (theValue => aValue,
          theName  => aName);
end Construct;

So I asked: In this case function Construct would behave as a C++ static function (or maybe a friend one?)?

And Dmitry Kazakov answered:

That depends on what you mean. In Ada:

  1. there is no hidden parameters

  2. an operation can be dispatching (virtual) in any combination of parameters and/or result. But an operation cannot be dispatching in
    more than one type (no multiple dispatch). All tags of dispatching
    parameters must be same (no multi-methods).

  3. there is no static or friend operations as the visibility rules are based on packages.

The function Construct above is a primitive operation, it is not a constructor.

Constructors in Ada are implicit, they consist of

  1. construction of the components (in an unspecified order, recursively);

  2. a call to Initialize if the type is a descendant of Ada.Finalization.[Limited_]Controlled. (Overridden bodies of
    Initialize are not called! I.e. Ada constructors do not traverse
    derivation path. In short, aggregation is safe, derivation is not;

  3. starting all task components. (Note, tasks are not running when Initialize is called!)

Destructors act in the reverse order: tasks – Finalize – components.

And I guess it responds. Thank you people.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T17:40:27+00:00Added an answer on May 26, 2026 at 5:40 pm

    About Ada constructors?

    It’s difficult to explain the concept as I have no idea of the philosophy, limitations and strengths of the Ada language. Still, guessing there is no constructors in Ada.

    A (non-friend?) non-member function?

    This is not the solution you want I guess:

    • having a Initialize member function of Par_Class, which sets the private data of Par_Class
    • having a Par_Class_Constructor non-member function calling that Initialize function

    But this solution is not satisfying because it would expose Initialize as a public method, which is a breach of encapsulation (anyone could call that method at any moment, which is almost as dumb as making all the data public).

    A static member function?

    What you want to do is allocate and initialize your code with but one function call, without breaching encapsulation.

    You feel (rightly) this function should be part of Par_Class interface, and thus, you want to declare it inside the Par_Class‘ declaration (which will have the interesting side-effect of giving it access to Par_Class private member variables)

    In Java or in C++, barring constructors, this could be resolved by having a static method, that is, a method of the class, instead of a method of the instance. This method is static, and so has no access to this meaning you can call it without needing an instance of Par_Class.

    So, your question to your Ada friends could be: is there a way to have a "static" function declared inside Par_Class?

    A (friend?) non-member function?

    Another way to have similar effect (if not similar syntactic sugar) would be to have a non-member function doing the trick. In C, you would have something like: Par_Class_Constructor, which would be a function returning the pointer to a struct of type Par_Class.

    In C++, you could use the same trick as Java, or the same trick as C. In that last case, Par_Class_Constructor would be declared friend of the class Par_Class to have access to its private data, or could call an initialization member method which have access to that private data.

    This way, you can still allocate and initialize your object with one function,and still protect the encapsulation of your class (as this method returns a new object, instead of modifying it like the unsatisfactory Initialize method described above)

    So, if having a non-member function is Ok for you, another possibility could be: is there a way to have an non-member function declared friend of Par_Class?

    Edit

    Note: my previous, out-of-topic answer… I really should go to bed…

    I don’t know Ada, but reading your code:

    with Par_Pkg; use Par_Pkg;
    procedure Par_Main is
       Par_Obj : Par_Class_Ptr;
       Int_Obj : Integer_Ptr;
    begin
       Int_Obj := new Integer;
       Int_Obj.all := 12; -- don t worry about been string or integer
       Par_Obj := Par_Obj.Construct 
         (aValue => 23,
          aName => Int_Obj);
    end Par_Main;
    

    I see Int_Obj has been allocated with the new Integer statement.

    Don’t you need to allocate Par_Obj the same way?

    Something like (I’m inferring from your Integer initialization code):

       Par_Obj := new Par_Class_Ptr      -- allocate ?
       Par_Obj.all := Par_Obj.Construct  -- initialize ?
         (aValue => 23,
          aName => Int_Obj);
    

    ???

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Maybe I just don't know .NET well enough yet, but I have yet to
Maybe the need to do this is a 'design smell' but thinking about another
Maybe I'm just thinking about this too hard, but I'm having a problem figuring
Maybe world peace would be easier, but I have a problem with the widths
OK so what's the problem with this. I tried using MySQL JConnector for Java
I am new to Java and have background of C.I am going through Khalid
Bear with me... I don't think this is too subjective but maybe I'm wrong.
im making a java mobile app to display quotes by people. It would have
I can't help it; I know many people will disagree, but I would really
So right now if I have something like this: //div[@class='artist']/p[x]/text() x can either be

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.