I now see some problems with your code (counted from worst to least bad).
You use std::malloc()
instead of using the new
As a C++ programmer you should use new
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
The operator delete
would call the destructor, while the function std::free()
By the way, you're not using std::free()
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.
Do not overuse dynamic memory allocation.
You can probably rewrite your program to be more elegant, without using so many pointers and memory allocations.
You mark your const
member functions the wrong way.
const void print() // wrong
void print() const // correct
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.
A(int ) // not giving the int parameter a name because we don't use it
explicit B(int )
void func_a(A )
void func_b(B )
func_a(A(100)); // works
func_a(200); // works!
func_b(B(100)); // works
func_b(200); // doesn't work!
So in your code below there is no reason to use explicit