|
-
March 10th, 2003, 08:33 PM
#4
Yes, you are right! Default constructor and destructor are generated if you did not declare.
When you just declare a node object (mynode), it is created on the stack. So when the function ends, the node object goes out-of-scope, thus it is destroyed. Therefore, the return value from CreateNodeWithData() is a pointer to a destroyed object (invalid pointer).
You can modify CreateNodeWithData() to create the node object from free-store using new keyword. In this case, you would have to use delete to destroy the object manually when not in use.
Code:
node* CreateNodeWithData( int data )
{
node* pNode = new node;
newnode->data = data;
newnode->next = null;
return pNode;
}
int main(void)
{
node* pNode = CreateNodeWithData(10);
delete pNode;
return 0;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|