I recently converted into the right faith (that being c++) from another language (of far worse reputation than java).
Anyways, a convert's advice: if you want to pass an object by address, it is perfectly legal, but the syntax includes an extra "&" in front of the object's name. So, your code lines 22-29 should look like:
1 2 3 4 5 6 7
|
Node(char o, Node& lNode, Node& rNode)
{
oper = o;
pLNode = &lNode;
pRNode = &rNode;
leaf = false;
}
|
The way you have written your function, when it is called, two new fake objects will be created for each of the arguments of your function. Those two objects will be given some temporary addresses in the memory, which will be recorded in pLNode and pRNode. After the function call is complete (I don't know precisely when), the two fake objects will be destroyed, thus leaving your two pointers pointing at memory that is not reserved by your program.
A natural question comes, why is this the default behavior for the shortest, "common sence" syntax you originally used? The answer I figured for myself is:
Because C++ is a horrible, outmoded, outdated, bloated aberration of a computer language...
...which also happens to be the best one available.
The operator "&" is in some sence the opposite of the operator "*". For example,
1 2 3 4 5 6 7 8 9 10 11 12 13
|
int a=5;
std::cout <<&a; //will return the physical address of the variable a
int b= *(&a); // b will be assigned the value 5
b=&(*a);
/*this will crash. It would first attempt to read the memory cell
of physical address 5 (which is outside the scope
that the system allows you to read)
If, however, the value of the variable a
was physical address to which your program
does have access, the previous line would not crash,
but make b=a instead.
*/
|
Please correct me if I am wrong!