Static class question

anonymous anonymous at example.com
Thu Feb 14 05:20:34 PST 2013


On Thursday, 14 February 2013 at 12:49:01 UTC, Lubos Pintes wrote:
> Hi,
> I saw one ListBox class implementation.
> As we all know, ListBox class collects and displays strings. 
> Author decided that he encapsulate the Object as item, and uses 
> its toString() method when string is needed for display.
> For cases when string is added, he defined this small class, 
> internal to ListBox:
> [code]
> 	private static class StringItem
> 	{
> 		private string _str;
>
> 		public this(string s)
> 		{
> 			this._str = s;
> 		}
>
> 		public override string toString()
> 		{
> 			return this._str;
> 		}
> 	}
> [/code]
>
> After I read about attributes on dlang, I think the static 
> could be removed from above definition. Is this true?
> Thank

Without 'static', StringItem objects would be tied to ListBox
objects. 'static' saves StringItem objects some bytes, and
ensures that StringItem is independent of ListBox.
See http://dlang.org/class.html#nested


More information about the Digitalmars-d-learn mailing list