我打算将一些用C / C编写的程序转换为Ada . 这些通常作为不规则的数组大量使用常量字符文字:
const char * stringsA = { "Up", "Down", "Shutdown" };
或记录中的字符串引用,如:
typedef struct
{
int something;
const char * regexp;
const char * errormsg
} ERRORDESCR;
ERRORDESCR edscrs [ ] =
{
{ 1, "regexpression1", "Invalid char in person name" },
{ 2, "regexp2", "bad bad" }
};
预设由C / C编译器计算,我希望Ada编译器也能够这样做 .
我使用谷歌搜索了不规则的数组,但只能找到两种预设字符串的方法 . 约翰巴恩斯为Ada 95提供基本原理,另一人为http://computer-programming-forum.com/44-ada/d4767ad6125feac7.htm . 这些在下面显示为stringsA和stringsB . StringsA分为两个阶段,如果要设置数百个字符串,这有点单调乏味 . StringsB仅使用一步,但依赖于编译器 .
问题1:还有其他方法吗?问题2:第二个字符串B是否可以与GNAT Ada一起使用?
我还没有开始转换 . 以下套餐仅供我自己进行实验和教学......
package ragged is
type String_ptr is access constant String;
procedure mydummy;
end ragged;
package body ragged is
s1: aliased constant String := "Up";
s2: aliased constant String := "Down";
s3: aliased constant String := "Shutdown";
stringsA: array (1 .. 3) of String_ptr :=
(s1'Access, s2'Access, s3'Access); -- works
stringsB: array (1 .. 3) of String_ptr :=
(new String'("Up"), new String'("Down"),
new String'("Shutdown")); -- may work, compiler-dependent
-- this would be convenient and clear...
--stringsC: array (1 .. 3) of String_ptr :=
-- ("Up", "Down", "Shutdown"); -- BUT Error, expected String_ptr values
--stringsD: array (1 .. 3) of String_ptr :=
--("Up"'Access, "Down"'Access, "Shutdown"'Access); --Error - bad Access use
--stringsE: array (1 .. 3) of String_ptr :=
--(String_ptr("Up"), String_ptr("Down"),
-- String_ptr("Shutdown")); -- Error, invalid conversion
procedure mydummy is
begin
null;
end;
end ragged;
2 回答
稍微明智的运算符重载可以以较简洁的方式执行此操作:
(在包体内)
现在你可以这样做:
没有足够的空间评论此测试程序