No need to have this if else , use customer(a,b) , in the constructor check if a>0 , b>0 if no set the default value.
ithelp
Nearly a Posting Maven
2,230 posts since May 2006
Reputation Points: 769
Solved Threads: 128
You declare local variables in if-else alternatives. Of course, they were discarded outside if-else statement:
if (a > 0 && b > 0) { // New block started
Account customer(a,b); // Local variable defined (on the stack)
} else { // Prev. bock is ended. Another block started.
Account customer; // Local variable defined
} // All customers dead... RIP...
// Alas, no customers here...
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
It's a good idea but not so good implementation. No need to define assignment operator: it's exactly default assignment. Keep it simpler:
Account customer;
...
if (a > 0 && b > 0)
customer = Account(a,b);
customer.somefunction();
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
Your problem is that the account objects are going out of scope when the if statement ends and their destructor's are being called. Initialized it outside of the if else statement and then just modify it as needed in the if statements. If you don't know what I mean by going out of scope just do a quick Google search. This is something every programmer should know about.
chunalt787
Junior Poster in Training
84 posts since Apr 2008
Reputation Points: 39
Solved Threads: 1