The following code compiles and runs fine till it reaches line 16 and gets a sigsev violation which I'm not sure about as to why. I have no problem passing the object of type node** into the constructor of base
and storing it into the double pointer node** copy;; but when I call the function void pass(node** temp) it crashes. I don't expect anyone to fix this but I just want to know why wouldn't this work?
#include <iostream>
class base;
class node
{
private:
node** data;
public:
node(){}
~node(){}
void pass(node** temp)
{
this->data = temp;//Step 4 sigsev violation???
}
};
class base
{
private:
node** copy;
public:
base(){}
base( node** temp)
{
(this->copy) = new node*();//Step 2 create an object to hold the node.
(this->copy) = temp;
(*this->copy)->pass(temp);//Step 3 pass the node from the int main into the new object.
}
~base(){}
};
int main()
{
node** temp = new node*();//Step 1 create a node of double pointer
base base_1(temp);//and pass it into the base object.
return 0;
}
On line 31 you overwrite the pointer to the object you just dynamically allocated.
You never actually create any instances of node, you only create pointers to pointers to it.
Ok so if I change this
1 2 3
(this->copy) = new node*();//Step 2 create an object to hold the node.
(this->copy) = temp;
(*this->copy)->pass(temp);//Step 3 pass the node from the int main into the new object.
to
1 2 3
(this->copy) = new node*();//Step 2 create an object to hold the node.
(*this->copy)->pass(temp);//Step 3 pass the node from the int main into the new object.