ab.com コミュニティ
https://www.activebasic.com/forum/

文字列リテラルで初期化できる変数
https://www.activebasic.com/forum/viewtopic.php?t=103
ページ 11

作成者:  イグトランス [ 2005年6月11日(土) 23:43 ]
記事の件名:  文字列リテラルで初期化できる変数

http://www.discoversoft.net/forum/viewtopic.php?t=100
ここのを読んでふと思いついた即席クラスです。
コード:
Class PSTR
Public
	Ptr As BytePtr
	Sub PSTR(p As BytePtr)
		Ptr = p
	End Sub
	
	Function MakeStr() As String
		MakeStr = MakeStr(Ptr)
	End Function
End Class

Class AutoBytePtr
	Inherits PSTR
Public
	Sub AutoBytePtr(p As BytePtr)
		PSTR(p)
	End Sub

	Sub ~AutoBytePtr()
		free(Ptr)
	End Sub
End Class

Class ByteBufStr
	Inherits AutoBytePtr
Public
	Sub ByteBufStr(pStr As BytePtr)
		AutoBytePtr(malloc(lstrlen(pStr) + 1))
		lstrcpy(Ptr, pStr)
	End Sub
End Class
PSTRは単に変数宣言と同時に文字列リテラルへのポインタで初期化できるようにしただけ。
AutoBytePtrはスマートポインタの機能を持たせた。
ByteBufStrは文字列のコピーを作ってそこへのポインタを保持させるようにした。
といった具合です。

そして例えばこんな風に使えます。
コード:
#prompt
Dim p1 As PSTR("Hello,")
Dim p2 As AutoBytePtr(malloc(4))
lstrcpy(p2.Ptr, "AB")
Dim p3 As ByteBufStr("World.")

Print p1.MakeStr(); p2.MakeStr(); p3.MakeStr()

ページ 11 全ての表示時間は UTC+09:00 です
Powered by phpBB® Forum Software © phpBB Limited
https://www.phpbb.com/