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:
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:
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:
there is no hidden parameters
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).
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
construction of the components (in an unspecified order, recursively);
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;
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.
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 (calling malloc()
if there's a new
, on the stack otherwise) 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 and is like having a C++ member function
Par_Class *a_Par_Class(int aValue, const char *aName);
which would fail in the same way as your current Ada code if called with an uninitialised Par_Class *
.
The nearest (Ada 2012) equivalent to the constructor 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/2005 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;
@paercebal mentioned that you need a static member function, which is correct; the C++ constructor is I think syntactic sugar for this. In Ada, scoping is done at the package level, so function Construct
- being declared in the public part of the same package and returning a Par_Class_Ptr
- is unmistakably (architecturally) associated with Par_Class
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With