www.digitalmars.com         C & C++   DMDScript  

D - Dumb questions on pointers and classes

reply Sean Kelly <sean ffwd.cx> writes:
Is there ever any reason to use a pointer?  If D classes really are 
refcounted then aren't they a needless abstraction except for 
interfacing with C libraries?  I ask this because I'm learning the ropes 
by implementing some basic container types, and the classsic 
implementations obviously use pointers :)

Also, is there any equivalent to "this" in C++?  The container I started 
on was a list, and circular reference sematics are kind of required. 
I'd like to be able to initialize nodes with "next" and "prev" pointing 
at the node itself.


Sean
Feb 10 2004
next sibling parent reply Sean Kelly <sean ffwd.cx> writes:
Quick update.  I tried using "this" as in C++ and got the message 
"cannot implicitly convert node to node*" so I figured I was on the 
right track.  I then tried using &this instead and the compiler crashed. 
  I've tried creating a simple repro and can't, but my list 
implementation is pretty basic so far.  I'm going to include it below in 
case someone else can point me at the problem.  To make this demo not 
crash, get rid of the ampersands in node.this():


template list(Ty)
{
	class node
	{
	public:
		this()
		{
			prev = &this;
			next = &this;
		}

		this( Ty v )
		{
			value = v;
			this();
		}

		Ty		value;
		node*	prev,
				next;
	};

	class iterator
	{
	public:
		this( node* n )
		{
			m_node = n;
		}

		iterator opAddAssign()
		{
			m_node = m_node.next;
			return this;
		}

		iterator opPostInc()
		{
			iterator tmp( m_node );
			opAddAssign();
			return tmp;
		}

		iterator opDecAssign()
		{
			m_node = m_node.prev;
			return this;
		}

		iterator opPostDec()
		{
			iterator tmp( m_node );
			opDecAssign();
			return tmp;
		}

		void value( Ty v )
		{
			m_node.value = v;
		}

		Ty value()
		{
			return m_node.value;
		}

	private:
		node*	m_node;
	}

	class cont
	{
	public:
		this()
		{
			m_head	= new node();
			m_size	= 0;
		}

		uint size()
		{
			reutrn m_size;
		}

		iterator begin()
		{
			return iterator( m_head.next );
		}

		iterator end()
		{
			return iterator( m_head );
		}

		iterator insert( iterator pos, Ty val )
		{
			node* n = new node( val );
			n.next = pos;
			n.prev = pos.prev;
			pos.prev.next = n;
			pos.prev = n;
			++m_size;
			return pos;
		}

	private:
		node*	m_head;
		uint	m_size;
	}
}


int main(char[][] args)
{
	alias list!(int) int_list;

	int_list.cont	mylist;

	mylist.insert( mylist.begin(), 1 );
	mylist.insert( mylist.begin(), 2 );
	mylist.insert( mylist.begin(), 3 );

	for( int_list.iterator i = mylist.begin(); i != mylist.end(); ++i )
	{
		printf( "%i\n", i.value );
	}
     return 0;
}
Feb 10 2004
next sibling parent Sean Kelly <sean ffwd.cx> writes:
Found the problem.  It was this function:

         iterator opPostInc()
         {
             iterator tmp( m_node );
             opAddAssign();
             return tmp;
         }
Apparently D doesn't like calling operator functions this way. Sean
Feb 10 2004
prev sibling parent Andy Friesen <andy ikagames.com> writes:
Sean Kelly wrote:

 Quick update.  I tried using "this" as in C++ and got the message 
 "cannot implicitly convert node to node*" so I figured I was on the 
 right track.  I then tried using &this instead and the compiler crashed. 
  I've tried creating a simple repro and can't, but my list 
 implementation is pretty basic so far.  I'm going to include it below in 
 case someone else can point me at the problem.  To make this demo not 
 crash, get rid of the ampersands in node.this():
You want to use node instead of node*. Classes are always handled by reference, like Java. -- andy
Feb 10 2004
prev sibling parent reply Andy Friesen <andy ikagames.com> writes:
Sean Kelly wrote:

 Is there ever any reason to use a pointer?  If D classes really are 
 refcounted then aren't they a needless abstraction except for 
 interfacing with C libraries?  I ask this because I'm learning the ropes 
 by implementing some basic container types, and the classsic 
 implementations obviously use pointers :)
D classes aren't refcounted. (I suppose they could be, but the current implementation uses mark&sweep GC instead) Pointers are used in D much like goto. D gives you lots of reasons not to need them, but still offers them if you really have no alternative. (interfacing with C is a big reason)
 Also, is there any equivalent to "this" in C++?  The container I started 
 on was a list, and circular reference sematics are kind of required. I'd 
 like to be able to initialize nodes with "next" and "prev" pointing at 
 the node itself.
Yes. It's called 'this'. :) -- andy
Feb 10 2004
parent Sean Kelly <sean ffwd.cx> writes:
Andy Friesen wrote:
 D classes aren't refcounted.  (I suppose they could be, but the current 
 implementation uses mark&sweep GC instead)
But it amounts to the same thing. There's no reason to use pointers in places where they'd be used in C/C++ :) It will take some getting used to, but I like it. Sean
Feb 10 2004