@long double main:
Thanks a lot! Your reply was insightful and of immense help! I have resolved the problem I had been dogged by for days.
Please, I have another nagging issue about
dynamic memory allocation. From my studying of C++, the lesson I have learnt about
d.m.a. is that any memory dynamically allocated has to be deallocated one way or another before exiting the environment/scope of the encompassing function. Let me furnish you with some background as it relates to the program here.
Another member function of my class (which I omitted in my first post) is
void showPostfix();
and its definition is simply
1 2 3 4
|
void postfixType::showPostfix()
{
cout<<postfixString;
}
|
In member function
convertToPostfix of my first post, one of the statements I omitted was
postfixString = pfx;
Now, let's say function
main is as shown below:
1 2 3 4 5 6 7 8
|
int main()
{
postfixType infxExpr(a+b);
infxExpr.convertToPostfix();
infxExpr.showPostfix();
return 0;
}
|
What I observed is that if I deallocated the dynamic memory allocated in function
convertToPostfix, that is by including statement
delete [] pfx;
the statement in
line 5 of function
main would not show anything; but if I
do not include the above memory-deallocating statement, the statement in
line 5 displays the correct postfix conversion! I understand why this is so
but I do not know how else to deallocate the memory in function
convertToPostfix without impairing the integrity of pointer data member
postfixString of the class.
Any insights please? While almost sounding illogical, I guess my question is how can I deallocate pfx while still ensuring that the statement
postfixString = pfx;
in function convertToPostfix still works?