template <class Type> class Queue {
Queue(): head(0), tail(0) {
cout << "Queue--default constructor called" << endl;
}
Queue(const Queue &Q): head(0), tail(0) {
cout << "Queue--copy constructor called" << endl;
//...
}
Queue& operator=(const Queue&) {
cout << "Queue--operator= called" << endl;
//...
}
~Queue() { //... }
private:
QueueItem<Type> *head;
QueueItem<Type> *tail;
};
I have defined a template class Queue and tried the codes below:
Queue<char*> cq;
Queue<char*> ccq(cq);
Queue<char*> acq = cq;
Queue<char*> acq2;
acq2 = cq;
and the output is:
Queue--default constructor called
Queue--copy constructor called
Queue--copy constructor called
Queue--default constructor called
Queue--operator= called
what confuse me is the code Queue<char*> acq = cq; has invoked the copy constructor Queue--copy constructor called but not the default constructor and operator= to be called.
Could any one help me?
Thank you for considering my question!
This is defined in the standard, it’s in the section
Explicit initialization
This is the case describing
single assignment-expressionandcopy-initialization semantics.Technically there’s no difference between the two forms
ccq(cq)andacq = cq, as you have already seen from the output.