Swap objects in a queue: How to define this data type?

I am writing the code below for method 'swap'.
Question is, if data type is "CharQueue2", how do I test this?
I am doing in main:
CharQueue2 q; //Create the object OK
q.swap("?") //How this source "CharQueue2" object would look like though?

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
#include <deque>
#include <iostream>

class CharQueue2 
{
    public:
        CharQueue2();
        CharQueue2(size_t size);
        void enqueue(char ch);
        char dequeue();
        bool isEmpty() const;
        void swap(CharQueue2 & src);
        size_t capacity() const;
    private:
        std::deque<char> vardeque;
};

CharQueue2::CharQueue2 () 
{
    vardeque = {};
}

void CharQueue2::swap(CharQueue2 & src) 
{
    CharQueue2 tmp_var = src;
    src = *this;
    *this = tmp_var;
}

//Implement enqueue; this will add a character in the Queue
void CharQueue2::enqueue (char ch) 
{
    vardeque.push_back(ch);
}

int main()
{
    //Create an object
    CharQueue2 q;
    //Now pass the argument to test it
    q.enqueue('a');

    //Test swap function
    CharQueue2 myvar = {}; //<= QUESTION: How to a "CharQueue2" object looks like?
    q.swap(myvar); 
    return 0;
}
I believe that line 44 calls the default constructor.
I believe I got it.
CharQueue2 q1;
CharQueue2 q2;
q1.swap(q2);
Apart from that, consider:
1
2
3
4
5
void CharQueue2::swap( CharQueue2 & src )
{
    // CharQueue2 has only one data member: vardeque
    this->vardeque.swap( src.vardeque );
}

Topic archived. No new replies allowed.