Classes in D and C++

Max Samukha samukha at voliacable.com
Mon Mar 5 02:10:53 PST 2007


On Mon, 05 Mar 2007 20:15:04 +1100, Daniel Keep
<daniel.keep.lists at gmail.com> wrote:

>
>
>Walter Bright wrote:
>> Uno wrote:
>>>> Its not possible it seems to do e.g this:
>>>>
>>>> X  x(3);
>>>>
>>>> rather you have to do:
>>>>
>>>> X x = new X(3);
>>>
>>> Yep, I don't like that syntax too. Everywhere news.. And although D
>>> has many great features such small things prevent me to switch to D.
>>>
>> 
>> You can do:
>> 
>>     auto x = X(3);
>> 
>> and x will be put on the stack.
>
>Surely you mean
>
>  scope x = X(3);
>
>Or did scope get rolled back into the auto keyword again while I wasn't
>looking? >_<

I think he means structs:

struct X{
	static X opCall( int n_in){
		X x;
		return x;		
	}
 }

void main()
{
	auto x = X(1); //allocates x of type X on stack and assigns
the result of X.opCall(1) to it.

	X x1 = 1; // does the same thing to x1

	x1 = cast(X)2; 	//calls X.opCall(2) and assigns the result to
x1;
}

Scope classes are allocated on stack but still require 'new'.

class X
{
	this(int n_in)
	{
	}

	~this()
	{
	}	
}

void test()
{
`	scope auto x = new X(1); // x allocated on stack	 
} //~this called on x on scope exit

void main()
{
	test();
}



More information about the Digitalmars-d mailing list