gpt4 book ai didi

ada - 使用原始操作创建对象

转载 作者:行者123 更新时间:2023-12-02 09:08:21 25 4
gpt4 key购买 nike

我的问题包含一段较长的 Ada 代码。基本上我尝试以面向对象的方式对带有标题的消息进行建模。两个原语操作Create_MessageCreate_Message_Access可用于创建具体的消息对象。我不太确定原始操作是否应返回类型 Message_TypeMessage_Type_Access。推荐哪种类型(效率?)或者这两种解决方案都不是最佳的?

我认为第一种方式是在堆栈上创建对象,然后在执行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;

我认为第二种方式是在堆上创建对象,然后在执行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;

后来我创建了一个带有标题的示例对象,使用原始操作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) 仅返回存储在向量中的消息的副本。对还是错?

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_TitleSecond_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?

我认为它会在返回语句期间被复制,但除此之外,是的(在某些涉及有限类型的情况下,您可以“就地构建对象”,但规则在语言标准版本之间发生了变化,所以我不确定;请参阅 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?

在本例中,没有。但如果过程是无参数的,则需要使用第一种形式,因为它显然不是子程序调用。


Element 这样的操作确实返回一个副本。如果您想更新容器中的元素,可以获取副本并修改它,然后使用 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 - 使用原始操作创建对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55364132/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com