I now see some problems with your code (counted from worst to least bad).
1) You use
std::malloc() instead of using the
new operator.
As a C++ programmer you should use
new as in:
temp = new node<T>;
.
The difference between them is that
new allocates the memory and then calls the constructor, while
std::malloc() just allocates the memory, without calling the constructor.
Same problems with
delete versus
std::free().
The operator
delete would call the destructor, while the function
std::free() would not.
2) By the way, you're not using
std::free() or
delete in your code.
This means that you have memory leaks.
A memory leak is when you allocate memory, and then don't deallocate it when you no longer need it.
Normally this just means your program "uses" more memory than it needs.
In very extreme cases, the computer will slow down or the system will crash.
https://en.wikipedia.org/wiki/Memory_leak
http://www.cplusplus.com/doc/tutorial/dynamic/
3) Do not overuse dynamic memory allocation.
You can probably rewrite your program to be more elegant, without using so many pointers and memory allocations.
4) You mark your
const member functions the wrong way.
http://www.parashift.com/c++-faq-lite/const-member-fns.html
1 2 3 4 5 6 7 8
|
const void print() // wrong
{
// ...
}
void print() const // correct
{
}
|
5) The
explicit keyword is used for constructors that take one parameter, and that parameter is not of the class' type, in this case
Queue.
The purpose of using
explicit is to prevent automatic conversions between types.
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
|
class A
{
public:
A(int ) // not giving the int parameter a name because we don't use it
{
}
};
class B
{
public:
explicit B(int )
{
}
};
void func_a(A )
{
}
void func_b(B )
{
}
int main()
{
func_a(A(100)); // works
func_a(200); // works!
func_b(B(100)); // works
func_b(200); // doesn't work!
}
|
http://ideone.com/Irrxxh
So in your code below there is no reason to use
explicit.
1 2 3 4
|
explicit Queue(){
list=0;
QueueSize=0;
}
|