Hello can we call the copy constructor inside the class ie? Ican\'t seem to figu
ID: 3609156 • Letter: H
Question
Hello can we call the copy constructor inside the class ie? Ican't seem to figure out how to do this.Any help would be greatly appreciated
#include <iostream>
#include "Stack.h"
using namespace std;
//*******************************
//** Copy constructor
//*******************************
Stack::Stack( const Stack& stack)
{
Node *copy = new Node;
Node *newNode = new Node;
Node *actual = new Node;;
copy = top;
actual = stack.top;
if(actual != NULL)
{
newNode -> data = actual->data;
top = newNode;
newNode ->next = NULL;
actual = actual ->next;
cSize++;
while(actual != NULL)
{cout<<actual->data<<" ";
Node *temp = new Node;
temp->data = actual ->data;
temp->next = top;
top = temp;
cSize++;
actual = actual->next;
}
}
}
//*******************************
//* Assigment overloaded
//*******************************
const Stack operator= (const Stack & rhs)
{
// Stack(rhs);
}
Explanation / Answer
Yeah you can just impliment the copy contructor so your assignmentoverload would look like this: void Stack::operator= ( const Stack& rhs ) const { if (this != rhs) { removeAll(); rhs.copy(); } }// end = operator for removeAll(); simply pop the stack until the header points tonull and "this" is the left hand side of the operation
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.