I'm having a terrible time figuring out how to write a copy constructor. As you can see below, we have a class with a struct nested inside of it for linked nodes to contain data. I can't use member-wise assignment to copy DynIntStack, because the pointers in the StackNode struct will just end up pointing to the same object.
Unfortunately, I can't figure out how to write the constructor such that it sets the value inside the new StackNode to be equal to the value of the StackNode inside the DynIntStack I'm passing the constructor.
I understand what I'm trying to do here, I just can't figure out how to write it out correctly.
class DynIntStack
{
private:
// Structure for stack nodes
struct StackNode
{
int value; // Value in the node
StackNode *next; // Pointer to the next node
};
StackNode *top; // Pointer to the stack top
public:
// Constructor
DynIntStack()
{
top = NULL;
}
// copy constructor
DynIntStack(DynIntStack &obj)
{
DynIntStack::StackNode value = obj::StackNode.value;
DynIntStack::StackNode next = new StackNode;
}
Try the following
If your compiler does not support this form of the new operator
when you can replace it with statements