Operator Overloading in C++
From: Artist (artist_at_sj.speakeasy.net)
Date: 09/25/04
- Next message: Edward G. Nilges: "Re: a benefit to using a linked list in insertion sort?"
- Previous message: Jim Rogers: "Re: Suggestion for review: a taxonomy of thread safety"
- Next in thread: Artist: "Re: Operator Overloading in C++"
- Reply: Artist: "Re: Operator Overloading in C++"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Fri, 24 Sep 2004 20:33:03 -0700
I am trying to write operator overload code for a class used in matrix
multiplication. The relevant code is pasted is below. On return from the
multiply operator code I get this error in MS Visual Studio .net Pro 2003:
Debug Assertion Failed!
Program: ...roj\Array Class.exe
File: dbgdel.cpp
Line: 52
Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)
I have discovered the problem is that in the operator*() function the "out"
object's destructor is being called on the function's return. There are
dynamic
arrays in the cDicastArray (not shown in the below code for clarity) class
that are deleted in the destructor. That the destructer is called is
logical because the out variable goes out of scope
upon the return from operator*(). Does this mean a class for which a binary
operator is defined cannot have a destructor? How is a deleted object
supposed to be passed to the equation that called this overloaded operator?
The return of this object having local scope in the definition of the
operator*() was done according examples in chapter 11 of the "C++ Programing
Language, Special Edition," by Bjarne Stroustrup, who of all people should
know how to do this. How is this supposed to work?
**** Code ****
cDicastArray<float> *Arr[3];
Arr[1] = new cDicastArray<float>();
Arr[2] = new cDicastArray<float>();
Arr[0] = &( *Arr[1] * *Arr[2] );
template<class T> cDicastArray<T> operator*(cDicastArray<T> left,
cDicastArray<T> right)
{ // Matrix multiply code omitted for clarity
cDicastArray<T> out( larger->getcDW(), outD ); // The output object.
// Matrix multiply code omitted for clarity
return out; // Debug Assertion Failed! error on this return
}
--- If you wish to respond to me directly remove the sj. in the domain name of my email address. This is a spam jammer.
- Next message: Edward G. Nilges: "Re: a benefit to using a linked list in insertion sort?"
- Previous message: Jim Rogers: "Re: Suggestion for review: a taxonomy of thread safety"
- Next in thread: Artist: "Re: Operator Overloading in C++"
- Reply: Artist: "Re: Operator Overloading in C++"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]