使用原始操作创建 objects
Creation of objects by using a primitive operation
我的问题包含一段较长的 Ada 代码。基本上,我尝试以面向 object 的方式为带有标题的消息建模。 Create_Message
和 Create_Message_Access
这两个原始操作可用于创建具体消息 object。我不太确定原始操作应该 return 类型 Message_Type
还是 Message_Type_Access
。推荐哪种类型(效率?)或者两种解决方案都不是最优的?
我认为第一种方式是在堆栈上创建 object,然后在 return
语句执行后复制,因为变量 Object
超出范围。对还是错?
function Create_Message (Title : String) return Message_Type is
Object : Message_Type;
begin
Object.Title := To_Unbounded_String (Title);
return Object;
end Create_Message;
我认为第二种方式是在堆上创建object,然后在执行return
语句后复制指针,因为变量 Object
超出范围。对还是错?
function Create_Message_Access (Title : String) return Message_Type_Access is
Object : Message_Type_Access := new Message_Type;
begin
Object.Title := To_Unbounded_String (Title);
return Object;
end Create_Message_Access;
后来我创建了一个带有标题的示例object,使用原始操作Updated_Title
更改标题并在最后将其更改回来。
First_Message := Message.Create_Message ("First");
Ada.Text_IO.Put_Line (First_Message.Get_Title);
First_Message.Update_Title ("First changed");
Ada.Text_IO.Put_Line (First_Message.Get_Title);
First_Message.Update_Title ("First");
Ada.Text_IO.Put_Line (First_Message.Get_Title);
输出是(如预期的):
First
First changed
First
然后我将消息保存在矢量容器中,并尝试在遍历存储在矢量中的元素时更改标题。我发现标题保留了旧值,所以我假设调用操作 Message_Vector.Element (Message_Cursor)
returns 只是向量中存储的消息的副本。对还是错?
Messages.Append (First_Message);
Message_Cursor := Message_Vector.First (Messages);
while Message_Vector.Has_Element (Message_Cursor) loop
declare
Temporary_Message : Message.Message_Type;
begin
Temporary_Message := Message_Vector.Element (Message_Cursor);
Temporary_Message.Update_Title ("First changed");
end;
Message_Vector.Next (Message_Cursor);
end loop;
Message_Cursor := Message_Vector.First (Messages);
while Message_Vector.Has_Element (Message_Cursor) loop
--
-- Prints "First" and not "First changed"
--
Ada.Text_IO.Put_Line (Message_Vector.Element (Message_Cursor).Get_Title);
Message_Vector.Next (Message_Cursor);
end loop;
最后一个问题:Second_Message.all.Get_Title
和Second_Message.Get_Title
有什么区别?
Second_Message := Message.Create_Message_Access ("Second");
Ada.Text_IO.Put_Line (Second_Message.all.Get_Title);
Ada.Text_IO.Put_Line (Second_Message.Get_Title);
完整源代码:
with Ada.Containers.Vectors;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with Ada.Text_IO;
procedure Main is
package Message is
type Message_Type is tagged private;
type Message_Type_Access is access Message_Type;
function Create_Message (Title : String) return Message_Type;
function Create_Message_Access (Title : String) return Message_Type_Access;
function Get_Title (Self : Message_Type) return String;
procedure Update_Title (Self : in out Message_Type; Title : String);
function "=" (Left, Right : Message_Type) return Boolean;
private
type Message_Type is tagged record
Title : Unbounded_String;
end record;
end Message;
package body Message is
function Create_Message (Title : String) return Message_Type is
Object : Message_Type;
begin
Object.Title := To_Unbounded_String (Title);
return Object;
end Create_Message;
function Create_Message_Access (Title : String) return Message_Type_Access is
Object : Message_Type_Access := new Message_Type;
begin
Object.Title := To_Unbounded_String (Title);
return Object;
end Create_Message_Access;
function Get_Title (Self : Message_Type) return String is
begin
return To_String (Self.Title);
end Get_Title;
procedure Update_Title (Self : in out Message_Type; Title : String) is
begin
Self.Title := To_Unbounded_String (Title);
end Update_Title;
function "=" (Left, Right : Message_Type) return Boolean is
begin
return Left.Title = Right.Title;
end "=";
end Message;
package Message_Vector is new Ada.Containers.Vectors (Index_Type => Natural,
Element_Type => Message.Message_Type, "=" => Message."=");
package Message_Access_Vector is new Ada.Containers.Vectors (Index_Type => Natural,
Element_Type => Message.Message_Type_Access, "=" => Message."=");
Messages : Message_Vector.Vector;
Message_Cursor : Message_Vector.Cursor;
Messages_Access : Message_Access_Vector.Vector;
Message_Access_Cursor : Message_Access_Vector.Cursor;
First_Message : Message.Message_Type;
Second_Message : Message.Message_Type_Access;
begin
First_Message := Message.Create_Message ("First");
Ada.Text_IO.Put_Line (First_Message.Get_Title);
First_Message.Update_Title ("First changed");
Ada.Text_IO.Put_Line (First_Message.Get_Title);
First_Message.Update_Title ("First");
Ada.Text_IO.Put_Line (First_Message.Get_Title);
--
Ada.Text_IO.New_Line;
Messages.Append (First_Message);
Message_Cursor := Message_Vector.First (Messages);
while Message_Vector.Has_Element (Message_Cursor) loop
declare
Temporary_Message : Message.Message_Type;
begin
Temporary_Message := Message_Vector.Element (Message_Cursor);
Temporary_Message.Update_Title ("First changed");
end;
Message_Vector.Next (Message_Cursor);
end loop;
Message_Cursor := Message_Vector.First (Messages);
while Message_Vector.Has_Element (Message_Cursor) loop
--
-- Prints "First" and not "First changed"
--
Ada.Text_IO.Put_Line (Message_Vector.Element (Message_Cursor).Get_Title);
Message_Vector.Next (Message_Cursor);
end loop;
--
Ada.Text_IO.New_Line;
Second_Message := Message.Create_Message_Access ("Second");
Ada.Text_IO.Put_Line (Second_Message.all.Get_Title);
Ada.Text_IO.Put_Line (Second_Message.Get_Title);
--
Ada.Text_IO.New_Line;
Messages_Access.Append (Second_Message);
Message_Access_Cursor := Message_Access_Vector.First (Messages_Access);
while Message_Access_Vector.Has_Element (Message_Access_Cursor) loop
declare
Temporary_Message : Message.Message_Type_Access;
begin
Temporary_Message := Message_Access_Vector.Element (Message_Access_Cursor);
Temporary_Message.Update_Title ("Second changed");
end;
Message_Access_Vector.Next (Message_Access_Cursor);
end loop;
Message_Access_Cursor := Message_Access_Vector.First (Messages_Access);
while Message_Access_Vector.Has_Element (Message_Access_Cursor) loop
Ada.Text_IO.Put_Line (Message_Access_Vector.Element (Message_Access_Cursor).Get_Title);
Message_Access_Vector.Next (Message_Access_Cursor);
end loop;
end Main;
The two primitive operations Create_Message and Create_Message_Access can be used to create concrete message objects. I'm not really sure if the primitive operation should return the type Message_Type or Message_Type_Access. Which type is recommended (efficiency?) or are both solutions are not optimal?
哪个最好,要视情况而定。一般来说,最好避免使用指针,但如果您所代表的是一个在您的系统中具有永久性的对象,那么您只需要拥有它的一个副本,正如您所发现的那样。我已经限制了它们,并使用了指针容器。
I think in the first way the object is created on the stack and then copied after the return statement is executed because the variable Object goes out of scope. Right or wrong?
我认为它会在 return 语句期间被复制,但除此之外,是的(在某些情况下,您可以 'build the object in place' 涉及有限的类型,但规则在语言标准版本,所以我不确定;请参阅 AARM 7.6(17.1) 并准备好感到困惑)。
I think in the second way the object is created on the heap and then the pointer is copied after the return statement is executed because the variable Object goes out of scope. Right or wrong?
再一次,是的。
... I assume calling the operation Message_Vector.Element (Message_Cursor) returns only a copy of the message stored in the vector. Right or wrong?
是的。
Last question: What is the difference between Second_Message.all.Get_Title and Second_Message.Get_Title?
在这种情况下,none。但如果过程是无参数的,则需要使用第一种形式,因为它显然不是子程序调用。
像Element
这样的操作确实是return一个副本。如果你想更新容器中的元素,你可以复制一份,修改它,然后使用Replace_Element
覆盖原来的。
您可能会发现使用 Update_Element
:
不那么麻烦(但仅仅是)
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Containers.Vectors;
procedure Updating_Messages is
type Message is record
N : Integer := 0;
end record;
package Message_Vectors is
new Ada.Containers.Vectors (Index_Type => Positive,
Element_Type => Message);
Messages : Message_Vectors.Vector;
begin
Messages.Append (Message'(N => 21)); -- sorry about this, failing of Google syntax highlighting '
Messages.Append (Message'(N => 42)); --'
for Cursor in Messages.Iterate loop
declare
procedure Increment (It : in out Message) is
begin
It.N := It.N + 1;
end Increment;
begin
Messages.Update_Element (Cursor, Increment'Access); --'
end;
end loop;
for M of Messages loop -- these are copies
Ada.Text_IO.Put_Line (M.N'Image); --'
end loop;
end Updating_Messages;
我的问题包含一段较长的 Ada 代码。基本上,我尝试以面向 object 的方式为带有标题的消息建模。 Create_Message
和 Create_Message_Access
这两个原始操作可用于创建具体消息 object。我不太确定原始操作应该 return 类型 Message_Type
还是 Message_Type_Access
。推荐哪种类型(效率?)或者两种解决方案都不是最优的?
我认为第一种方式是在堆栈上创建 object,然后在 return
语句执行后复制,因为变量 Object
超出范围。对还是错?
function Create_Message (Title : String) return Message_Type is
Object : Message_Type;
begin
Object.Title := To_Unbounded_String (Title);
return Object;
end Create_Message;
我认为第二种方式是在堆上创建object,然后在执行return
语句后复制指针,因为变量 Object
超出范围。对还是错?
function Create_Message_Access (Title : String) return Message_Type_Access is
Object : Message_Type_Access := new Message_Type;
begin
Object.Title := To_Unbounded_String (Title);
return Object;
end Create_Message_Access;
后来我创建了一个带有标题的示例object,使用原始操作Updated_Title
更改标题并在最后将其更改回来。
First_Message := Message.Create_Message ("First");
Ada.Text_IO.Put_Line (First_Message.Get_Title);
First_Message.Update_Title ("First changed");
Ada.Text_IO.Put_Line (First_Message.Get_Title);
First_Message.Update_Title ("First");
Ada.Text_IO.Put_Line (First_Message.Get_Title);
输出是(如预期的):
First
First changed
First
然后我将消息保存在矢量容器中,并尝试在遍历存储在矢量中的元素时更改标题。我发现标题保留了旧值,所以我假设调用操作 Message_Vector.Element (Message_Cursor)
returns 只是向量中存储的消息的副本。对还是错?
Messages.Append (First_Message);
Message_Cursor := Message_Vector.First (Messages);
while Message_Vector.Has_Element (Message_Cursor) loop
declare
Temporary_Message : Message.Message_Type;
begin
Temporary_Message := Message_Vector.Element (Message_Cursor);
Temporary_Message.Update_Title ("First changed");
end;
Message_Vector.Next (Message_Cursor);
end loop;
Message_Cursor := Message_Vector.First (Messages);
while Message_Vector.Has_Element (Message_Cursor) loop
--
-- Prints "First" and not "First changed"
--
Ada.Text_IO.Put_Line (Message_Vector.Element (Message_Cursor).Get_Title);
Message_Vector.Next (Message_Cursor);
end loop;
最后一个问题:Second_Message.all.Get_Title
和Second_Message.Get_Title
有什么区别?
Second_Message := Message.Create_Message_Access ("Second");
Ada.Text_IO.Put_Line (Second_Message.all.Get_Title);
Ada.Text_IO.Put_Line (Second_Message.Get_Title);
完整源代码:
with Ada.Containers.Vectors;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with Ada.Text_IO;
procedure Main is
package Message is
type Message_Type is tagged private;
type Message_Type_Access is access Message_Type;
function Create_Message (Title : String) return Message_Type;
function Create_Message_Access (Title : String) return Message_Type_Access;
function Get_Title (Self : Message_Type) return String;
procedure Update_Title (Self : in out Message_Type; Title : String);
function "=" (Left, Right : Message_Type) return Boolean;
private
type Message_Type is tagged record
Title : Unbounded_String;
end record;
end Message;
package body Message is
function Create_Message (Title : String) return Message_Type is
Object : Message_Type;
begin
Object.Title := To_Unbounded_String (Title);
return Object;
end Create_Message;
function Create_Message_Access (Title : String) return Message_Type_Access is
Object : Message_Type_Access := new Message_Type;
begin
Object.Title := To_Unbounded_String (Title);
return Object;
end Create_Message_Access;
function Get_Title (Self : Message_Type) return String is
begin
return To_String (Self.Title);
end Get_Title;
procedure Update_Title (Self : in out Message_Type; Title : String) is
begin
Self.Title := To_Unbounded_String (Title);
end Update_Title;
function "=" (Left, Right : Message_Type) return Boolean is
begin
return Left.Title = Right.Title;
end "=";
end Message;
package Message_Vector is new Ada.Containers.Vectors (Index_Type => Natural,
Element_Type => Message.Message_Type, "=" => Message."=");
package Message_Access_Vector is new Ada.Containers.Vectors (Index_Type => Natural,
Element_Type => Message.Message_Type_Access, "=" => Message."=");
Messages : Message_Vector.Vector;
Message_Cursor : Message_Vector.Cursor;
Messages_Access : Message_Access_Vector.Vector;
Message_Access_Cursor : Message_Access_Vector.Cursor;
First_Message : Message.Message_Type;
Second_Message : Message.Message_Type_Access;
begin
First_Message := Message.Create_Message ("First");
Ada.Text_IO.Put_Line (First_Message.Get_Title);
First_Message.Update_Title ("First changed");
Ada.Text_IO.Put_Line (First_Message.Get_Title);
First_Message.Update_Title ("First");
Ada.Text_IO.Put_Line (First_Message.Get_Title);
--
Ada.Text_IO.New_Line;
Messages.Append (First_Message);
Message_Cursor := Message_Vector.First (Messages);
while Message_Vector.Has_Element (Message_Cursor) loop
declare
Temporary_Message : Message.Message_Type;
begin
Temporary_Message := Message_Vector.Element (Message_Cursor);
Temporary_Message.Update_Title ("First changed");
end;
Message_Vector.Next (Message_Cursor);
end loop;
Message_Cursor := Message_Vector.First (Messages);
while Message_Vector.Has_Element (Message_Cursor) loop
--
-- Prints "First" and not "First changed"
--
Ada.Text_IO.Put_Line (Message_Vector.Element (Message_Cursor).Get_Title);
Message_Vector.Next (Message_Cursor);
end loop;
--
Ada.Text_IO.New_Line;
Second_Message := Message.Create_Message_Access ("Second");
Ada.Text_IO.Put_Line (Second_Message.all.Get_Title);
Ada.Text_IO.Put_Line (Second_Message.Get_Title);
--
Ada.Text_IO.New_Line;
Messages_Access.Append (Second_Message);
Message_Access_Cursor := Message_Access_Vector.First (Messages_Access);
while Message_Access_Vector.Has_Element (Message_Access_Cursor) loop
declare
Temporary_Message : Message.Message_Type_Access;
begin
Temporary_Message := Message_Access_Vector.Element (Message_Access_Cursor);
Temporary_Message.Update_Title ("Second changed");
end;
Message_Access_Vector.Next (Message_Access_Cursor);
end loop;
Message_Access_Cursor := Message_Access_Vector.First (Messages_Access);
while Message_Access_Vector.Has_Element (Message_Access_Cursor) loop
Ada.Text_IO.Put_Line (Message_Access_Vector.Element (Message_Access_Cursor).Get_Title);
Message_Access_Vector.Next (Message_Access_Cursor);
end loop;
end Main;
The two primitive operations Create_Message and Create_Message_Access can be used to create concrete message objects. I'm not really sure if the primitive operation should return the type Message_Type or Message_Type_Access. Which type is recommended (efficiency?) or are both solutions are not optimal?
哪个最好,要视情况而定。一般来说,最好避免使用指针,但如果您所代表的是一个在您的系统中具有永久性的对象,那么您只需要拥有它的一个副本,正如您所发现的那样。我已经限制了它们,并使用了指针容器。
I think in the first way the object is created on the stack and then copied after the return statement is executed because the variable Object goes out of scope. Right or wrong?
我认为它会在 return 语句期间被复制,但除此之外,是的(在某些情况下,您可以 'build the object in place' 涉及有限的类型,但规则在语言标准版本,所以我不确定;请参阅 AARM 7.6(17.1) 并准备好感到困惑)。
I think in the second way the object is created on the heap and then the pointer is copied after the return statement is executed because the variable Object goes out of scope. Right or wrong?
再一次,是的。
... I assume calling the operation Message_Vector.Element (Message_Cursor) returns only a copy of the message stored in the vector. Right or wrong?
是的。
Last question: What is the difference between Second_Message.all.Get_Title and Second_Message.Get_Title?
在这种情况下,none。但如果过程是无参数的,则需要使用第一种形式,因为它显然不是子程序调用。
像Element
这样的操作确实是return一个副本。如果你想更新容器中的元素,你可以复制一份,修改它,然后使用Replace_Element
覆盖原来的。
您可能会发现使用 Update_Element
:
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Containers.Vectors;
procedure Updating_Messages is
type Message is record
N : Integer := 0;
end record;
package Message_Vectors is
new Ada.Containers.Vectors (Index_Type => Positive,
Element_Type => Message);
Messages : Message_Vectors.Vector;
begin
Messages.Append (Message'(N => 21)); -- sorry about this, failing of Google syntax highlighting '
Messages.Append (Message'(N => 42)); --'
for Cursor in Messages.Iterate loop
declare
procedure Increment (It : in out Message) is
begin
It.N := It.N + 1;
end Increment;
begin
Messages.Update_Element (Cursor, Increment'Access); --'
end;
end loop;
for M of Messages loop -- these are copies
Ada.Text_IO.Put_Line (M.N'Image); --'
end loop;
end Updating_Messages;