"August1" <> wrote in message
news: m...
> "> No, the problem is that you haven't not defined an assignment
> operator for
> > your class. You use it right after the operator- but the compiler
generated
> > on you are using does not do the right thing.
> >
> > Whenever you have a destructor, you almost always need to provide a copy
> > constructor AND an assignment operator. This is called the 'rule of
three'.
> >
> > john
>
> Hi John,
>
> Thanks for the response. I added an overloaded assignment operator
> to see what would take place, and this did resolve the problem with
> program execution. However, it still does not perform as intended.
> Specifically, the value derived from subtracting one operand from the
> other does not appear to be assigned to the data member within the
> overloaded subtraction operator that is assigned to the temporary
> class object and returned. When I use the shares.getNumShares()
> function at the end of the client (user) source file, I always get a 0
> value which is incorrect.
Possibly you did not implement the assignment operator correctly. I notice
that your copy constructor is not implemented correctly.
Stocks::Stocks(const Stocks& sourceStock)//copy constructor definition
{
szStockName = new char[25];
strcpy(szStockName, sourceStock.szStockName);
iNumShares = 0;
dCurrentValue = 0;
dPricePerShare = 0;
};
It should be
Stocks::Stocks(const Stocks& sourceStock)//copy constructor definition
{
szStockName = new char[25];
strcpy(szStockName, sourceStock.szStockName);
iNumShares = sourceStock.iNumShares ;
dCurrentValue = sourceStock.dCurrentValue ;
dPricePerShare = sourceStock.dPricePerShare ;
};
In other words the only thing being copied in the copy constructor was the
stock name, everything else was being set to zero. Maybe you made the same
mistake in the assignment operator.
> Also, the information concerning the rule of 3 is very helpful. I
> would think that the more detail that is added to a class to offer the
> client greater diversity would routinely necessitate the use of copy
> constructors, destructors for pointer variables specifically among
> other cleanup details, in addition to overloaded assignment,
> subtraction, addition, increment, and decrement operator functions
> among others. I think it is a course that I will take with most
> subsequent programs.
> Unfortunately, I find myself again becoming more disappointed in
> both the text I am using and the author.
Which book are you using?
> The rule of 3 is never
> mentioned, nor any of the reasons for this that you have begun to
> signify. There are some good and useful things in the text, but I
> think that most people in general have a higher standard of
> expectation.
>
> anthony
john
|