U
Universe
Hello all. I have a problem about initialization of member variable.
The example code is below:
#include "iostream"
using namespace std;
class ParentsNode
{
public:
ParentsNode();
~ParentsNode();
private:
class Node;
Node* aTestNode;
};
class ParentsNode::Node
{
public:
Node() {
Node("");
};
Node(string s) : s(s), next(NULL)
{
cout << "The value of Node.next when Node is
initialing: " << next << endl;
};
public:
string s;
Node* next;
};
ParentsNode:arentsNode()
{
aTestNode = new Node;
cout << "The value of Node.next when ParentsNode is initialing: "
<< aTestNode->next << endl;
}
ParentsNode::~ParentsNode()
{
delete aTestNode;
aTestNode = NULL;
};
int main()
{
ParentsNode* pSimpleNode = new ParentsNode;
delete pSimpleNode;
return 0;
}
Output:
Debug mode,
The value of Node.next when Node is initialing: 00000000
The value of Node.next when ParentsNode is initialing: CDCDCDCD
Release mode,
The value of Node.next when Node is initialing: 00000000
The value of Node.next when ParentsNode is initialing: D50891A6
The environment is Win7+VC2008SP1.
I am confused with the value of Node.next. Why it's changed after
inialized in constructor Node(string s). It doesn't seem the inialized
value was successfully preserved when return to constructor Node().
Does it happen on other C++ compiler? Does C++ stardard define this
exactly?
Please give me some hints. Thank you very much.
The example code is below:
#include "iostream"
using namespace std;
class ParentsNode
{
public:
ParentsNode();
~ParentsNode();
private:
class Node;
Node* aTestNode;
};
class ParentsNode::Node
{
public:
Node() {
Node("");
};
Node(string s) : s(s), next(NULL)
{
cout << "The value of Node.next when Node is
initialing: " << next << endl;
};
public:
string s;
Node* next;
};
ParentsNode:arentsNode()
{
aTestNode = new Node;
cout << "The value of Node.next when ParentsNode is initialing: "
<< aTestNode->next << endl;
}
ParentsNode::~ParentsNode()
{
delete aTestNode;
aTestNode = NULL;
};
int main()
{
ParentsNode* pSimpleNode = new ParentsNode;
delete pSimpleNode;
return 0;
}
Output:
Debug mode,
The value of Node.next when Node is initialing: 00000000
The value of Node.next when ParentsNode is initialing: CDCDCDCD
Release mode,
The value of Node.next when Node is initialing: 00000000
The value of Node.next when ParentsNode is initialing: D50891A6
The environment is Win7+VC2008SP1.
I am confused with the value of Node.next. Why it's changed after
inialized in constructor Node(string s). It doesn't seem the inialized
value was successfully preserved when return to constructor Node().
Does it happen on other C++ compiler? Does C++ stardard define this
exactly?
Please give me some hints. Thank you very much.