www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Using static opCall instead of the default constructor.

reply "Jaehunt" <jaeyoep gmail.com> writes:
Hello, I am trying to using static opCall instead of the default 
constructor.

When I run it, I got "Error: need 'this' to access member A".

How to fix it?

Here is my code.

struct PriorityQueue(T) {
	static const int DEFAULT_QueueSIZE = 1;
	T[] A;
	int num;
	
	this(this){
		A = A.dup;
	}
	
	static PriorityQueue opCall() {
		
		A =  new T[DEFAULT_QueueSIZE];  // Error
		num = 1;                        // Error
		
		PriorityQueue priorityQueue;
		
		return priorityQueue;
	}
	
	bool empty() { return num == 0; }
	
	void insert( T item) {
		if( A.length == num ) A.length *= 2;
		A[ num++] = item;
	}
	
	T extractMax() {
		A.sort;
		return A[ --num];
	}
}
Jul 16 2013
parent reply "Namespace" <rswhite4 googlemail.com> writes:
----
static PriorityQueue opCall(T)() {
	PriorityQueue!T po;

	po.A = new T[DEFAULT_QueueSIZE];
	po.num = 1;

	return po;
}
----
Jul 16 2013
parent "Jaehunt" <jaeyoep gmail.com> writes:
On Tuesday, 16 July 2013 at 23:00:06 UTC, Namespace wrote:
 ----
 static PriorityQueue opCall(T)() {
 	PriorityQueue!T po;

 	po.A = new T[DEFAULT_QueueSIZE];
 	po.num = 1;

 	return po;
 }
 ----
Thanks a lot.
Jul 16 2013