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:
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.
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:
Initializemember function ofPar_Class, which sets the private data ofPar_ClassPar_Class_Constructornon-member function calling that Initialize functionBut this solution is not satisfying because it would expose
Initializeas 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_Classinterface, and thus, you want to declare it inside thePar_Class‘ declaration (which will have the interesting side-effect of giving it access toPar_Classprivate 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 tothismeaning you can call it without needing an instance ofPar_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 astructof 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_Constructorwould be declaredfriendof the classPar_Classto 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
Initializemethod 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
friendofPar_Class?Edit
Note: my previous, out-of-topic answer… I really should go to bed…
I don’t know Ada, but reading your code:
I see Int_Obj has been allocated with the
new Integerstatement.Don’t you need to allocate Par_Obj the same way?
Something like (I’m inferring from your Integer initialization code):
???