Queue Template Help / General Questions

I'm working on a queue template, here it is below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//CLASS DEFINITION SHOULD BE HERE...
template <class T>	//Template constructor
Queue<T> :: ~ Queue() //passed maxsize from initial declaration of stack
{
	delete [] q_ptr;	//deletes the queue
}

template <class T> //Template add function
void Queue<T> :: add (T element)	//passed an element t add to queueu
{
	if (!isFull())	//if isFull function is not true (not full)
	{
		rear = (rear + 1) % arraylength;	//rear goes to next available spot
		q_ptr [rear] = element;	//add the element to this open spot
	}
	else
	{
			cout << "Queue is full";
	}
}

template <class T> //Template delete function
void Queue<T> :: deleteIt ()
{
	if (!isEmpty())	//if isEmpty function is not true (not empty)
	{
		front = (front+1) % arraylength; 	//front increments to next spot
											// if it hits the rear, rebounds to zero again
		q_ptr[front].~T();	//delete this spot in memory
	}
	else
	{
		cout << "Queue is empty!" << endl;
	}
	void add(T element);
}

int main()
{
	Queue <int> nums(10);
	int data;
	
	nums.add(1);
	nums.add(2);	
	nums.add(3);
	nums.add (4);
	
	nums.deleteIt();
	nums.deleteIt();
	
	cout << nums.currSize();
	
	return 0;
}


Just a couple quick questions:

1- Can someone explain the syntax and process of line 61... not sure what it does, using code from my textbook.
 
	q_ptr[front].~T();	//delete this spot in memory 


2- In that same function why are we calling line 67?
 
	void add(T element);


Thanks so much.
Last edited on
1) you calling a destructor of object in q_ptr array with index front.
2) This is a forward declaration. Inside function. Does nothing.
As MiiNiPaa says, line 61 is calling the destructor of an object, however it should not do so, because it will result in undefined behavior. The only thing that function should do is adjust the front index.

IMO, add would be better named push_back or enqueue, and deleteIt would be better named pop_front or dequeue.



MiiNiPaa (740) Mar 28, 2013 at 1:25am
1) you calling a destructor of object in q_ptr array with index front.
2) This is a forward declaration. Inside function. Does nothing.


1- I thought you could not delete a single object in C++, only the whole array.

2- Makes sense, even though I put the add function before..


cire (2814) Mar 28, 2013 at 3:18am
As MiiNiPaa says, line 61 is calling the destructor of an object, however it should not do so, because it will result in undefined behavior. The only thing that function should do is adjust the front index.

IMO, add would be better named push_back or enqueue, and deleteIt would be better named pop_front or dequeue.


Can you elaborate....Sorry a bit confused.
1
2
3
4
delete foo;//deletes one object
delete[] bar;//deletes array if bar points to start of memory block allocated with new[]
baz.~Baz();//calling destructor of type Baz. Note, that does not deallocate memory baz was using!
           //It merely destroys an object (will free any memory manually allocated in constructor) 


Some standard containers do that to delete additional memory which was allocated to stored object, but preserve part of underlying container memory, where main body of class was, from deallocating (when calling erase() on std::vector or do something like vec[0] = foo;)
http://stackoverflow.com/questions/14187006/is-calling-destructor-manually-always-a-sign-of-a-bad-design

1
2
3
4
5
6
7
//Part of std::vector.erase
      --this->_M_impl._M_finish;
      this->_M_impl.destroy(this->_M_impl._M_finish);
      return __position;
//new allocator.h
      void 
      destroy(pointer __p) { __p->~_Tp(); }

Last edited on
Can you elaborate....Sorry a bit confused.


About which part? The naming thing, I think, should be fairly obvious. add and deleteIt are poorly named.

As far as the calling of the destructor, it will result in undefined behavior because the destructor will be called at least twice. The queue implementation makes no effort to ensure that doesn't happen as do the standard containers MiiNiPaa referred to.

Topic archived. No new replies allowed.