Is it the right approach to define copy constructor, copy assignment operator, move constructor, and move assignment operator without actually invokin

  • Thread starter Thread starter bhavya internship
  • Start date Start date
B

bhavya internship

Guest
Hello all,

In one of my classes, I am implementing constructor, destructor, copy constructor, copy assignment operator, move constructor, and move assignment operator.

But as of now I am using only constructor and destructor.
Might be from some other places we can call copy constructor or assignment operator, and other functions. Or else at the later point of time in future whoever wants to create new object and assign it to the existing object that time they can use the definition of copy constructor / assignment operator that i already did. This is the instruction that i got from the management.

Can anyone please suggest, Is it the right approach to define copy constructor or assignment operator without using those?

Is it the correct way to delete the member variable (delete tstQueue;) before moving using Move assignment operator?

Below is the code snippet:

class TestMessage
{
public:
TestMessage();
~TestMessage() {}
};

class TestQueue
{
private:
TestMessage* tstQueue;

public:
~TestQueue();
TestQueue();
TestQueue(const TestQueue &tQueue);
TestQueue& operator= (const TestQueue &tQueue);
TestQueue(TestQueue &&tQueue);
TestQueue& operator= (TestQueue &&tQueue);
};

// Constructor
TestQueue::TestQueue()
{

}

// Destructor
TestQueue::~TestQueue()
{
if (nullptr != tstQueue)
{
delete tstQueue;
tstQueue = nullptr;
}
}

// Copy constructor
TestQueue::TestQueue(const TestQueue &tQueue)
{
//tstQueue = tQueue.tstQueue;
*tstQueue = *tQueue.tstQueue;
}

//Copy Assignment operator
TestQueue& TestQueue::operator = (const TestQueue &tQueue)
{
if (this == &tQueue)
return *this;

tstQueue = tQueue.tstQueue;

return *this;
}

// Move constructor
TestQueue::TestQueue(TestQueue &&tQueue)
: tstQueue(tQueue.tstQueue)
{
tQueue.tstQueue = nullptr;
}

// Move assignment
TestQueue& TestQueue::operator = (TestQueue&& tQueue)
{
if (&tQueue == this)
return *this;

delete tstQueue;

tstQueue = tQueue.tstQueue;
tQueue.tstQueue = nullptr;

return *this;
}


int _tmain(int argc, _TCHAR* argv[])
{
TestQueue *pobj = new TestQueue();
return 0;
}

Continue reading...
 
Back
Top